Laravel5 关联查询问题

关联查询一致报错,不得已来SF麻烦各位大神帮我解答。
报错内容
ErrorExceptionin777fb9d9b961f2b2453d93297ba8a847line15:
Tryingtogetpropertyofnon-object(View:/home/millyn/www/x.x/resources/views/NewHome.blade.php)
我的对应文件如下
Avatar.php
useIlluminate\Database\Eloquent\Model;
classAvatarextendsModel{
publicfunctionarticle()
{
return$this->hasMany('App\Article');
}
}
Article.php
useIlluminate\Database\Eloquent\Model;
classArticleextendsModel{
//
publicfunctionavatar()
{
return$this->belongsTo('App\Avatar');
}
}
Controller.php
publicfunctionindex()
{
$articles=Article::with('avatar')->latest()->paginate(8);
returnview('NewHome',compact('articles'));
}
NewHome.blade.php
@foreach($articlesas$page)
title}}"href="{{URL('news/'.$page->id)}}">

{{$page->title}}

title}}"href="{{URL('news/'.$page->id)}}">avatars_id->url}}"alt=""width="180px">
@endforeach
{!!$articles->render()!!}
article_table.php
publicfunctionup()
{
Schema::create('articles',function(Blueprint$table)
{
$table->increments('id');
$table->string('title');
$table->text('body')->nullable();
$table->string('site')->nullable();
$table->integer('avatars_id')->unsigned();;
$table->integer('user_id');
$table->string('nike')->nullable();
$table->timestamps();
$table->foreign('avatars_id')
->references('id')
->on('avatars')
->onDelete('cascade');
});
}
avatars_table.php
publicfunctionup()
{
Schema::create('avatars',function(Blueprint$table)
{
$table->increments('id');
$table->string('title');
$table->string('url');
$table->text('path');
$table->timestamps();
});
}
我不知道是哪里有问题,不管怎么弄都是报错..希望大神帮忙看看.谢谢了.

			
偶然的你
浏览 306回答 2
2回答

湖上湖

你可以dd($articles)来看看,相信在relations这一个上面会是null,总感觉你的relationship声明好奇怪
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript