dianikol85
4 years ago

Laravel 5 relationships

Posted 4 years ago by dianikol85

I'm trying to reproduce the "Digging in" series using laravel 5 but having issues with relationships

class User extends Model implements UserContract, RemindableContract {

 use UserTrait, RemindableTrait;

 /**
  * The database table used by the model.
  *
  * @var string
  */
 protected $table = 'users';

 /**
  * The attributes excluded from the model's JSON form.
  *
  * @var array
  */
 protected $hidden = ['password', 'remember_token'];

    protected $fillable = ['username','email','password'];

    public function tasks() {
        return $this->hasMany('Task');
    }

}

class Task extends Model {

 protected $fillable = ['title','body','user_id','completed'];

    public function user() {
        return $this->belongsTo('User');
    }
}

 public function show($id)
    {

        try {
            $task = $this->tasks->findOrFail($id);

            return $task->user;
        } catch (ModelNotFoundException $e) {
            return ':(';
        }
    }

It gives me that error

Class 'User' not found

Any thoughts?

Please sign in or create an account to participate in this conversation.