关联查询一致报错,不得已来SF麻烦各位大神帮我解答。
报错内容
ErrorException in 777fb9d9b961f2b2453d93297ba8a847 line 15:
Trying to get property of non-object (View: /home/millyn/www/x.x/resources/views/NewHome.blade.php)
我的对应文件如下
Avatar.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Avatar extends Model {
public function article()
{
return $this -> hasMany('App\Article');
}
}
Article.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
//
public function avatar()
{
return $this -> belongsTo('App\Avatar');
}
}
Controller.php
public function index()
{
$articles = Article::with('avatar')->latest()->paginate(8);
return view('NewHome', compact('articles'));
}
NewHome.blade.php
<div class="container">
<div class="row">
@foreach ($articles as $page)
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading" style="white-space:nowrap;overflow:hidden;text-overflow:clip;">
<a title="{{ $page->title }}" href="{{ URL('news/'.$page->id) }}"> <h4>{{ $page->title }}</h4></a>
</div>
<div class="panel-body">
<a title="{{ $page->title }}" href="{{ URL('news/'.$page->id) }}"><img src="{{$page->avatars_id->url}}" alt="" width="180px"></a>
</div>
</div>
</div>
@endforeach
</div>
</div>
<div class="col-md-12">
{!!$articles->render()!!}
</div>
article_table.php
public function up()
{
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
public function up()
{
Schema::create('avatars', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->string('url');
$table->text('path');
$table->timestamps();
});
}
我不知道是哪里有问题,不管怎么弄都是报错..希望大神帮忙看看.谢谢了.