Ad
Laravel - How To Get Rows Of Permissions From Another Table Set In Roles Table
I am very new to laravel and trying to get the all permission rows from the permissions
table that set into the roles
- permission
field as a json
Obviously, this is not working as aspected. It returns an array of collections. However, I want each permission rows that associate with the roles results in itself.
public function index()
{
$roles = Role::all();
foreach ($roles as $role) {
$permissions[] = Permission::whereIn('id', json_decode($role->permission))->get();
}
dd($permissions);
}
Permissions table
Roles table
Ad
Answer
It is recommended to extract the permission field to a separate table. This would make it possible to implement constraints to ensure the referenced permission id and role id do actually exists. A possible table name would be: permission_role
with the following columns: permission_id
and role_id
.
The migration could be setup like this:
Schema::create('permission_role', function (Blueprint $table) {
$table->unsignedBigInteger('permission_id');
$table->unsignedBigInteger('role_id');
$table->foreign('permission_id')->references('id')->on('permissions');
$table->foreign('role_id')->references('id')->on('roles');
});
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms
Ad