Hello Jeff,
Thanks for this tutorial, It has made my sojourn into Laravel much more easier.
I have an issue with Eloquent, When I try to access an attribute of the related User object from an Article object like this;
$article = Article::findOrFail($id);
$article->user->name;
I get this error, what could be wrong?:
LogicException in Model.php line 2641:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
in Model.php line 2641
at Model->getRelationshipFromMethod('user') in Model.php line 2572
at Model->getAttribute('user') in Model.php line 3231
at Model->__get('user') in ArticlesController.php line 129
at ArticlesController->show(object(Article))
--- Models ---
namespace App\Models;
class Article extends Model {
public function user(){
$this->belongsTo('App\User');
}
}
namespace App;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
public function articles(){
return $this->hasMany('App\Models\Article');
}
}
--- Migration ---
Schema::table('articles', function(Blueprint $table) {
...
$table->integer('user_id')->unsigned();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});