Answer the question
In order to leave comments, you need to log in
How to call a model method in Laravel?
I continue to learn Laravel. I work with resources.
public function toArray($request) {
return [
'mainMenu' => CategoryResource::collection(Category::all()),
'navigation' => '',
'cities' => '',
'status' => 'success',
];
}
public function index() {
return $this->where('actf', 1)->get();
}
public function toArray($request) {
return [
'mainMenu' => CategoryResource::collection([Category::class, 'index']),
'navigation' => 2,
'cities' => 3,
'status' => 'success',
];
}
public function toArray($request) {
return [
'title' => $this->title,
'url' => '/catalog/' . $this->url,
];
}
Answer the question
In order to leave comments, you need to log in
Well, if you ask for everything Category::all()
, then everything is spit out for you.
Request only active
'mainMenu' => CategoryResource::collection(Category::where('enabled', true)->get()),
Delete initial. Delete InitialResource. Because it is not a resource. Pass data via json response. Inside which CategoryResource. For the correct request, the Query Builder uses the where method. If you want to call a method from the model, do scope. Without get in the model. Pass as written in the documentation
CategoryResource::collection($categories);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question