I need to put the equivalent of an 'as' in a with subquery in an eloquent controller since I need to get properties from the same child table.
$userInfo = auth()->user()->id;
$projects = Project::withCount(['project_contents' => function($query){
$query->where('complete', 1);
}, 'project_images'])
->with(['project_images','project_contents'])
->where('user_id', $userInfo)->orderBy('id', 'DESC')-get();
As you can see in the previous code, I am doing two with since I need to obtain the complete properties of those child tables, and I am also doing two withCount to the same tables to obtain the number of columns that belong to the project, in the project_contents table I am doing making a query to get the project_contents that are completed, but I also need to get the project_contents that have another 'type' property that is equal to 'link'.
What I came up with was to do something like this in the same withCount:
withCount(['project_contents' => function($query){
$query->where('complete', 1);
}, 'project_contents'=> function ($query){
$query->where('type', 'link');}, 'project_images'])
but when getting the data from count eloquent doesn't know which count to do since the two queries are directed to the same table and in vuejs it is called with the same name:
project.project_contens_count
emphasizing that in vuejs I made a v-for of projects and call the property project.project_contents_count
By default the name of the count will have this nomenclature:
relacion_count
, which we get when we do this:However Eloquent allows us to rename the result of the relation count like this:
So your query segment can look like this: