Search code examples
phplaraveleloquentrelationship

Laravel eloquent search for name relation


Okay, so I'm a bit stuck. I've created a group system in my project. The group system is 5 models:

  1. Group
  2. GroupCategory
  3. GroupMember
  4. GroupPost
  5. GrouPostComment

What I'm trying to do is a search for members in a group by username input. But I only store the user_id in the GroupMember model, and not username which is stored in the User model. I do not have any relations to groups in the User model yet.

Is there some kind of relation I have to put in? I've not come to understand the Morph relation yet.

Here are a list of the relations for the Group some of the models needed.

Group model

public function category() {
    return $this -> belongsTo('App\GroupCategory', 'category_id');
}

public function owner() {
    return $this -> hasOne('App\GroupMember', 'group_id') -> where('owner', true);
}

public function mods() {
    return $this -> hasMany('App\GroupMember', 'group_id') -> where('mod', true);
}

public function members() {
    return $this -> hasMany('App\GroupMember', 'group_id');
}

public function posts() {
    return $this -> hasMany('App\GroupPost', 'group_id');
}

public function comments() {
    return $this -> hasMany('App\GroupPostComment', 'group_id');
}

GroupMember model

public function groups() {
    return $this -> belongsToMany('App\Group');
}

public function posts() {
    return $this -> hasMany('App\GroupPost');
}

public function comments() {
    return $this -> hasMany('App\GroupPostComment');
}

public function user() {
    return $this -> belongsTo('App\User');
}

Just to clarify. I have an input field where the admin can type in a letter or parts of the username he's looking for, then it does a AJAX search where it looks for only members in the group, but it looks by username. And username is not in the GroupMember model, but in the User model.

How would I do this?

Thanks in advance.

Edit: The controller method.

 /**
 * @param Group $group
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 *
 * Manage users
 */
public function getManageUsers(Group $group) {

    $group = $group -> with('members.user');
    
    // Usernames can be "test0", "test1", "test2"
    // If i search "1" only "test1" should be in the result
    $yourSearchQuery = '1';

    // The query
    $result = $group -> whereHas('members', function ($query) use ($yourSearchQuery) {
        $query -> whereHas('user', function ($query) use ($yourSearchQuery) {
            $query -> where('name', 'LIKE', '%'. $yourSearchQuery .'%');
        });
    }) -> get();

    // Check the results manually
    // It still returns all members of the group
    dd($result);

}

Solution

  • So I finally fixed it! Just had to modify the answer from Odin Thunder a bit.

    This was the query that works.

    $result = $group -> members() -> whereHas('user', function($query) use($q) {
        $query -> where('name', 'LIKE', '%'. $q .'%');
    }) -> get();