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

I have a search field which acts to filter down a list of AuthorProfiles. Each author profile has a few

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);
   }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745296644a4621182.html

相关推荐

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

    I have a search field which acts to filter down a list of AuthorProfiles. Each author profile has a few

    15小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信