Answer the question
In order to leave comments, you need to log in
How to add calculated field to eloquent selection in Laravel?
Good evening. Tricky question. How to make that during selection of the list of models by a method
The connected User models had a field which is calculated during selection. That is, somehow assign a method
to it. This is necessary to search for the associated model by user name. $Projects = Project::with('user')->all();
selectRaw("CONCAT(f_name, f_name) as name")
Answer the question
In order to leave comments, you need to log in
$projects = Project::with(['user' => function ($query) {
return $query->selectRaw('users.*, CONCAT(users.f_name, users.f_name) as name');
}])->all();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question