lundi 3 octobre 2016

Method roles not found in laravel Auth

I keep getting the method not found error when I'm trying to assign a role to the user in the user registration section.

The following is the code that I am using in the AuthController

namespace App\Http\Controllers\Auth;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

use App\User;

class AuthController extends Controller
{

protected function create(array $data)
{
    $user =  User::create([
        'first_name'        => $data['first_name'],
        'last_name'         => $data['last_name'],
        'email'             => $data['email'],


    ])->get();

    $user->roles()->sync([2]);      

    return $user;
}
}

following is my User model

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;    
use Zizaco\Entrust\HasRole;    
use App\Role;

class User extends Authenticatable
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'users';
    protected $fillable = [
        'first_name','last_name', 'email', 'password'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function roles()
    {
        return $this->belongsToMany('App\Role');
    }


}

I'm not sure why this is happening. I think maybe it's not importing the User model properly, or I'm missing something. I'm new to laravel, so any help will be welcome.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire