mardi 5 janvier 2016

Laravel 5 Seeding Pivot Tables

I was watching the following lesson and was following along with Roles and Permissions however now I"m to seeding my pivot table with a Seeder class but not sure how I should accomplish this.

http://ift.tt/1M0lGmV

This is the sort of thing I have right now but obviously with the givePermissionTo() inside of the Role model I'm sure there is a better way of handling this. I have a feeling I don't even need a PermissionRole model. The big issue here is that I run it for all 5 different roles and about 63 different permissions.

<?php

use Illuminate\Database\Seeder;
use App\PermissionRole;

class PermissionRoleTableSeeder extends Seeder
{
    public function run()
    {
        PermissionRole::create([
            'role_id' => 5,
            'permission_id' => '1',
        ]);

        PermissionRole::create([
            'role_id' => 5,
            'permission_id' => '2',
        ]);

        PermissionRole::create([
            'role_id' => 5,
            'permission_id' => '3',
        ]);
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{

    public function permissions()
    {
        return $this->belongsToMany(Permission::class);
    }

    public function givePermissionTo(Permission $permission)
    {
        return $this->permissions()->save();
    }

    public function users()
    {
        return $this->hasMany(User::class);
    }
}

Can someone hint as to what that is?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1MR38CS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire