accessing custom method in model

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


accessing custom method in model



Im practicing laravel and im making a custom method for my user



In my user model i have build a function like this


public function employee(){
return $this->where('user_type','employee');
}



and then in my controller I'm accessing the function like this


public function index(){
$users = User::latest()->employee();
return UserResource::collection($users);
}



but it return an error Method IlluminateDatabaseQueryBuilder::employee does not exist.how to fix this?


IlluminateDatabaseQueryBuilder::employee does not exist.




1 Answer
1



Use local scope instand


public function scopeEmployee($query)
{
return $query->where('user_type', 'employee');

}



Your controller can be as it was !


public function index(){
$users = User::latest()->employee()->get();
return ProductsResource::collection($users);
}





error Method IlluminateDatabaseQueryBuilder::mapInto does not exist.
– Beginner
yesterday





maybe the user model is not extending the Model class?
– Beginner
yesterday





Please, check it and let me know about it or may be, You forgot just to call Model::get()...
– Goms
yesterday




Model::get()





i tried it already
– Beginner
yesterday





ProductsResource::collection() need a collection as argument, it may be the reason of this error. Try to use the code I edited above
– Goms
yesterday








By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS