dimanche 23 juin 2019

laravel eager loading relationship with where clause not working

I want to load that users that have a company_id = 28. I put where clause but its not working, collection returning all records.

MyController.php

public function index($type, $company_id = '')
{
    $user = User::whereHas('roles', function ($query) use ($type) {
        $query->where('name', '=', $type);
    })->get();

    $users = $user->load(['userSetting' => function ($query) {
        $query->where('company_id', '28');
    }]);

    return response()->json($users);
}

User.php

public function userSetting()
{
   return $this->hasOne('App\UserSettings', 'user_id');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire