model file
class BanUser extends Model
{
public function user()
{
return $this->belongsTo(User::class, "user_id", "id");
}
public static function getBanList()
{
$getlist = BanUser::with(['user'])
->get()
->toArray();
return $getlist;
}
}
//data
[0] => Array
(
[id] => 3
[user_id] => 2
[created_at] => 2020-09-16T16:06:54.000000Z
[updated_at] => 2020-09-16T16:06:54.000000Z
[user] => Array
(
[id] => 2
[name] => hi
[email] => [email protected]
[email_verified_at] =>
[created_at] => 2020-09-10T12:18:18.000000Z
[updated_at] => 2020-09-10T12:19:11.000000Z
)
)
The above code is to retrieve all the ban user list, but every user have attached one role with it, how can i join the role table in order to retrieve the role by every user? I used the laravel spatie permission. Anyone can help on this? :(
i guess you can use this
public function user()
{
return $this->belongsTo(User::class, "user_id", "id")->with('roles');
}
this roles
is added by laravel spatie
so you can use this
this is roles model https://github.com/spatie/laravel-permission/blob/master/src/Models/Role.php
all the available function which you can use https://github.com/spatie/laravel-permission/blob/master/src/Traits/HasRoles.php
in another way
public static function getBanList()
{
$getlist = BanUser::with(['user.roles']) // it will get user as well as all the roles of that user
->get()
->toArray();
return $getlist;
}