i have create user roles and permissions using Laravel 5.8. i created users and roles correctly, but when i am creating many to many model relationship between permissions and roles, an error occurred and said "Call to undefined function permissions()". i have created a getPermissions() method to retrieve all permissions according to role id. But also laravel said that method need to be static. i do not know what is the reason. why the permissions() relationship method not working?
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Auth;
class Role extends Model
{
public function users()
{
return $this->belongsToMany('App\Models\AdminUser', 'role_user', 'role_id', 'user_id');
}
public function permissions()
{
return $this->belongsToMany('App\Models\Permission', 'permission_role', 'role_id',
'permission_id');
}
public static function getPermissions()
{
if(Auth::guard('adminuser')->check()){
$permissions = "Dashboard, Appointments, Customer Records";
$admin_user_id = Auth::guard('adminuser')->user()->id;
$role_id = 1;//roles()->select('id')->where('roles.id', $admin_user_id)->first();
$per = permissions()->select($permissions)->where('permissions.id', $role_id)->first();
return $per;
}
}
}
Thanks in Advance!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire