accessing custom method in model
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);
}
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.
error Method IlluminateDatabaseQueryBuilder::mapInto does not exist.
– Beginner
yesterday