Queries that return a Laravel resource keep returning a 500 error.
P粉293341969
P粉293341969 2023-07-27 10:11:11
0
1
334

I'm trying to build an array of details to pass to a dashboard resource, but I'm having some issues trying to pass the data to the resource.

In my controller I am building the query like this:

$user = auth()->user(); $teams = Team::query() ->where('user_id', $user->id) ->get(); $jobs = Job::query() ->where('user_id', $user->id) ->get(); return new DashboardResource($user, $jobs, $teams); 

Then go to the resource

public function toArray(Request $request): array { return [ 'name' => $user->name, 'teams' => TeamResource::collection($this->teams), 'jobs' => JobResource::collection($this->jobs), ]; } 

I have been encountering a problem, that is;

Property [jobs] does not exist on this collection instance.

I think I may have done something wrong.

P粉293341969
P粉293341969

reply all (1)
P粉497463473

The JsonResource constructor only accepts one parameter. You should create an association between Teams and Jobs in the user model, and then call $user->teams and $user->jobs in the resource.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!