admin管理员组文章数量:1418627
I have a search field which acts to filter down a list of AuthorProfiles. Each author profile has a few of its own values (author_profile
, author_slug
) and is associated with one User.
The User model has a function called getName()
which returns a concatenation of the user's first_name and surname fields.
I'm fine with live filtering the list of author profiles ($authors
, in the below code) to return only those whose profile or slug match the search term. But won't necessarily match include the User's first name or surname, unless they are also used in the profile / slug, which may not be a given.
if($this->search != "") {
$authors = $authors->where('author_profile', 'like', '%' . $this->search . '%')->orWhere('author_slug', 'like', '%' . $this->search . '%');
}
How could I filter the above further to include authors (a) for whom the getName() function would match the search term, or (b) for whom their first name or surname would match the search term.
Functionally the same thing, I realise, in this example, but I'd like to know, for future reference, how to filter based on a relationship function and a relationship value.
I have a search field which acts to filter down a list of AuthorProfiles. Each author profile has a few of its own values (author_profile
, author_slug
) and is associated with one User.
The User model has a function called getName()
which returns a concatenation of the user's first_name and surname fields.
I'm fine with live filtering the list of author profiles ($authors
, in the below code) to return only those whose profile or slug match the search term. But won't necessarily match include the User's first name or surname, unless they are also used in the profile / slug, which may not be a given.
if($this->search != "") {
$authors = $authors->where('author_profile', 'like', '%' . $this->search . '%')->orWhere('author_slug', 'like', '%' . $this->search . '%');
}
How could I filter the above further to include authors (a) for whom the getName() function would match the search term, or (b) for whom their first name or surname would match the search term.
Functionally the same thing, I realise, in this example, but I'd like to know, for future reference, how to filter based on a relationship function and a relationship value.
Share Improve this question edited Jan 29 at 13:05 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Jan 29 at 12:58 Giles BennettGiles Bennett 1,6361 gold badge13 silver badges17 bronze badges 1 |1 Answer
Reset to default 0Due to the lack of information on your model name, I assume the AuthorProfile
Model has a belongsTo
relationship to the User
Model with the method name author
on the AuthorProfile
model.
$search = $this->search;
$authors
->where('author_profile', 'like', '%' . $search . '%')
->orWhere('author_slug', 'like', '%' . $search . '%');
->orWhereHas('author', function($query) use($search) {
$query
->where('first_name', 'like', '%' . $search . '%')
->orWhere('surname', 'like', '%' . $search . '%');
});
class AuthorProfile extends Model
{
public function author() : BelongsTo
{
return $this->belongsTo(User::class);
}
}
本文标签: laravelHow to filter results by relationship functionStack Overflow
版权声明:本文标题:laravel - How to filter results by relationship function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745296643a2652118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
getName()
function. – TEFO Commented Feb 1 at 8:24