lundi 29 mai 2017

Laravel two wherehas queries not working

I would like to assign a group to users which have a role student and have also a particular selected group. There is a users table, which has pivot tables: role_user and group_user with roles and groups tables. Below is the code for the controller where I am trying to execute the query:

    $this->validate($request, [
        'add-group'      => 'required',
        'where-group'    => 'required'
    ]);

    $selectedGroup       = $request->input('add-group');
    $whereGroupId        = $request->input('where-group');

    $users         = User::whereHas(
                        'roles', function($q){
                            $q->where('name', 'student');
                        }
                    )->whereHas(
                        'groups', function($q){
                            $q->where('id', $whereGroupId);
                        }
                    )->get();

    $selectedGroup    = Group::whereId($selectedGroup)->first();
    $users->assignGroup($selectedGroup); 



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2rcgFU8
via IFTTT

Aucun commentaire:

Enregistrer un commentaire