jeudi 19 mai 2016

Laravel - Call to undefined method in repository

I created a EloquentUserRepository in order to keep my code clean and extensible.

This is my EloquentUserRepository:

namespace App\Repositories\User;

use App\Models\Role;
use App\Repositories\EloquentRepository;

class EloquentUserRepository extends EloquentRepository implements UserContract
{

    /**
     * Get class name
     *
     * @return string
     */
    protected function getModelName()
    {
        return 'App\Models\User';
    }

    /**
     * Check if user has the given role.
     * Could be a string or a collection
     *
     * @param $role
     */
    public function hasRole($role)
    {
        if (is_string($role)) {
            return $this->roles->contains('name', $role);
        }

        // This will remove from the roles all the roles that do not match the given one.
        // If the result is empty the user do not have that role.
        return $role->intersect($this->roles)->count();
    }

    /**
     * Assign the role to the user
     *
     * @param Role $role
     */
    public function assignRole(Role $role)
    {
        return $this->model->roles()->save($role);
    }
}

But when I use it in my Seeder:

use App\Models\Role;
use App\Models\User;
use App\Repositories\User\EloquentUserRepository;
use Illuminate\Database\Seeder;

class AdminSeeder extends Seeder
{

    /**
     * @var EloquentUserRepository
     */
    private $userRepository;

    /**
     * AdminSeeder constructor.
     *
     * @param EloquentUserRepository $userRepository
     */
    public function __construct(EloquentUserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    /**
     * Run the database seeds.
     *
     */
    public function run()
    {
        $admins = [
            [
                'first_name' => 'Christian',
                'last_name'  => 'Giupponi',
                'email'      => 'christian.giupponi@example.com',
                'password'   => 'sviluppo'
            ]
        ];
        $adminRole = Role::where('name', 'admin')->get()->first();

        foreach($admins as $admin){
            $a = $this->userRepository->create($admin);
            $a->assignRole($adminRole);
        }
    }
}

I get:

BadMethodCallException] Call to undefined method Illuminate\Database\Query\Builder::assignRole()

This is the create method:

/**
     * Create new resource
     *
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return $this->model->create($data);
    }

if I dd I get the User model:

App\Models\User {#605
  #fillable: array:4 [
    0 => "first_name"
    1 => "last_name"
    2 => "email"
    3 => "password"
  ]
  #hidden: array:2 [
    0 => "password"
    1 => "remember_token"
  ]
  #dates: array:1 [
    0 => "deleted_at"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:7 [
    "first_name" => "Christian"
    "last_name" => "Giupponi"
    "email" => "christian.giupponi@example.com"
    "password" => "$2y$10$http://ift.tt/1XBtKS6"
    "updated_at" => "2016-05-19 07:12:18"
    "created_at" => "2016-05-19 07:12:18"
    "id" => 1
  ]
  #original: array:7 [
    "first_name" => "Christian"
    "last_name" => "Giupponi"
    "email" => "christian.giupponi@example.com"
    "password" => "$2y$10$http://ift.tt/1XBtKS6"
    "updated_at" => "2016-05-19 07:12:18"
    "created_at" => "2016-05-19 07:12:18"
    "id" => 1
  ]
  #relations: []
  #visible: []
  #appends: []
  #guarded: array:1 [
    0 => "*"
  ]
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: true
  #forceDeleting: false
}

Edit:
Log of $a over iterations:

[2016-05-19 07:22:12] local.DEBUG: App\Models\User Object
(
    [fillable:protected] => Array
        (
            [0] => first_name
            [1] => last_name
            [2] => email
            [3] => password
        )

    [hidden:protected] => Array
        (
            [0] => password
            [1] => remember_token
        )

    [dates:protected] => Array
        (
            [0] => deleted_at
        )

    [connection:protected] => 
    [table:protected] => 
    [primaryKey:protected] => id
    [perPage:protected] => 15
    [incrementing] => 1
    [timestamps] => 1
    [attributes:protected] => Array
        (
            [first_name] => Christian
            [last_name] => Giupponi
            [email] => christian.giupponi@example.com
            [password] => $2y$10$aW/rP7oHqH0/mj7RfYBR9OdvxRCEXiYLOVCIvfY1BHGNCuBNdcy/i
            [updated_at] => 2016-05-19 07:22:12
            [created_at] => 2016-05-19 07:22:12
            [id] => 1
        )

    [original:protected] => Array
        (
            [first_name] => Christian
            [last_name] => Giupponi
            [email] => christian.giupponi@example.com
            [password] => $2y$10$aW/rP7oHqH0/mj7RfYBR9OdvxRCEXiYLOVCIvfY1BHGNCuBNdcy/i
            [updated_at] => 2016-05-19 07:22:12
            [created_at] => 2016-05-19 07:22:12
            [id] => 1
        )

    [relations:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [appends:protected] => Array
        (
        )

    [guarded:protected] => Array
        (
            [0] => *
        )

    [dateFormat:protected] => 
    [casts:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [with:protected] => Array
        (
        )

    [morphClass:protected] => 
    [exists] => 1
    [wasRecentlyCreated] => 1
    [forceDeleting:protected] => 
)



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

Aucun commentaire:

Enregistrer un commentaire