Pritika
Pritika

Reputation: 306

How to connect already connected table with other table in laravel?

I have a user table it has one userRole and userRole belongs to Role. So, I want to fetch the userRole and Role also.

Code in user Model:

public function userRole()
{
    return $this->hasOne(UserRole::class);
}

Code in UserRole model:

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

Code in controller:

User::with('userRole', function ($role) {
  $role->with(['Role']);
})
->wherehas('userRole', function ($query) {
     $query->where('role_id','1');
 })->get();

This is giving me error

"mb_strpos() expects parameter 1 to be string"

Upvotes: 0

Views: 50

Answers (1)

Thomas Van der Veen
Thomas Van der Veen

Reputation: 3226

The problem is that you should pass an array when you want to add a constrait to the with() method.

Your code should like something like:

User::with([
    'userRole' => function ($query) {
        ...
    }
])
...

Upvotes: 1

Related Questions