I'm starting to work on an API service with Laravel. Now, I'm using a DDD approach (and learning it at the same time).
Currently my structure looks like this:
MyApp
- app
- (laravel app stuff)
- src
- Domain
- Category
- Actions
- DataTransferObjects
- Models
- Resources
- Category
- Domain
As you can see I'm currently using Resources. So for example, in my Categories' controller I've got:
public function index(): AnonymousResourceCollection
{
$categories = Category::all();
return CategoriesResource::collection($categories);
}
and my resource file looks like this:
<?php
namespace Domain\Category\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class CategoriesResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param Request $request
* @return array
*/
public function toArray($request): array
{
return [
'id' => (string)$this->id,
'type' => 'categories',
'attributes' => [
'name' => $this->name,
'parent' => $this->parent,
'description' => $this->description,
'image' => $this->image,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]
];
}
}
which returns the JSON response that will be eventually expected by the frontend.
Now, I've been reading about ModelView but still don't understand the concept or how could it work instead of (or along with) Resources. Most of the examples I've seen so far don't actually return a whole JSON response from the ModelView.
Thanks
They are 2 different things. Laravel
Resourcesare especially design for API responses, to transform your Eloquent models into JSON responses using any kind of logic that you may need. As simple as that (Like you're doing in your code).TL;DR:
Resource: Transform, add or remove attributes, load relationships, into a "JSON representation of your models (Orcollectionof models)".ModelView: It's just a file with a bunch of methods to be used in you view instead of passing each variable individually in every method of your controller.Long explanation:
Now, in the link you posted about
ModelViewthey give this examples as the ordinary way of passing data to a view:This "ViewModel" is just a way to encapsulate all of the logic and methods away from your controllers into a single file, which at the end, contains all the information passed to the view (Even data that you may not need).
And this, is how should be use in the view:
There are a few things I don't like about this approach (In this particular example):
$viewModel->categories()you're making querys to the database (Instead of passing $categories and calling it as many times as you want in the view without making new querys)current_user()and$postto theViewModeland than, passing all of that to the view.ModelView, but rather a simpleResource