vendredi 24 novembre 2017

Laravel: On Login: getAuthIdentifierName()

Laravel 5.4, issues with User Model. I am currently working myself into Laravel and have now stumpled across this issue.

I am successfully signing up a company and one associated user (it's a one-to-many relationship between the Models Owner and User). After signup, I want to sign the user in and redirect to a particular page. That's where I get the error:

Call to undefined method App\User::getAuthIdentifierName()

These are the models:

Owner

namespace App;

use Illuminate\Database\Eloquent\Model;

class Owner extends Model
{
  protected $fillable = ['firma', 'email', 'www'];

  public function scopeNumUsers($query, $pId){
    return User::where('owner_id', $pId)->count();
  }

  public function users()  {
    return $this->hasMany('App\User');
  }
}

User

namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Access\Authorizable;
use Illuminate\Auth\CanResetPassword;

class User extends Model
{
  protected $fillable = ['name', 'username', 'password', 'email', 'owner_id'];

  public function owner() {
    return $this->belongsTo('App\Owner');
  }
}

The OwnersController handles signup and login:

public function store(Request $request) {

      /* Validation */
      $this->validate(request(),[
        'firma' => 'required|regex:[^(?!.*Firma).*$]',
        'email' => 'required|email',
        'domain' => 'required',
        'name' => 'required|regex:[^(?!.*Name).*$]'
      ]);

      /* Save Owner */
      $ownerdata = [
        'firma' => $request->firma,
        'email' => $request->email,
        'www' => $request->domain,
      ];
      $owner = Owner::create($ownerdata);

      /* Save User */
      $user = new User([
        'name' => $request->name,
        'username' => $request->email,
        'email' => $request->email,
        'password' => Bcrypt('zaPasswordWillGoHere')
      ]);

      $owner->users()->save($user);

      /* Sign In User */
      \Auth::loginUsingId($user->id);
      /*
      \Auth::login($newUser);*/

      /* Redirect */
      return redirect('/nms');
    }

I get that Authenticatable seems to to be available to the User Model. But I am not getting why, considering I load it in.

Looking forward to be illuminated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire