最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

laravel - How to filter results by relationship function? - Stack Overflow

programmeradmin1浏览0评论

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
  • Please show the getName() function. – TEFO Commented Feb 1 at 8:24
Add a comment  | 

1 Answer 1

Reset to default 0

Due 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);
   }
}
发布评论

评论列表(0)

  1. 暂无评论