关联查询一致报错,不得已来sf麻烦各位大神帮我解答。
报错内容
<code>ErrorException in 777fb9d9b961f2b2453d93297ba8a847 line 15: Trying to get property of non-object (View: /home/millyn/www/x.x/resources/views/NewHome.blade.php) </code>
我的对应文件如下
Avatar.php
<code><?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Avatar extends Model {
public function article()
{
return $this -> hasMany('App\Article');
}
}
</code>Article.php
<code><?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
//
public function avatar()
{
return $this -> belongsTo('App\Avatar');
}
}
</code>Controller.php
JQuery是继prototype之后又一个优秀的Javascript库。它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+),jQuery2.0及后续版本将不再支持IE6/7/8浏览器。jQuery使用户能更方便地处理HTML(标准通用标记语言下的一个应用)、events、实现动画效果,并且方便地为网站提供A
55
<code> public function index()
{
$articles = Article::with('avatar')->latest()->paginate(8);
return view('NewHome', compact('articles'));
}
</code>NewHome.blade.php
<code> <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) }}">@@##@@avatars_id->url}}" alt="" width="180px"></a>
</div>
</div>
</div>
@endforeach
</div>
</div>
<div class="col-md-12">
{!!$articles->render()!!}
</div>
</code>article_table.php
<code> 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');
});
}
</code>avatars_table.php
<code> public function up()
{
Schema::create('avatars', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->string('url');
$table->text('path');
$table->timestamps();
});
}
</code>我不知道是哪里有问题,不管怎么弄都是报错..希望大神帮忙看看.谢谢了.
关联查询一致报错,不得已来sf麻烦各位大神帮我解答。
报错内容
<code>ErrorException in 777fb9d9b961f2b2453d93297ba8a847 line 15: Trying to get property of non-object (View: /home/millyn/www/x.x/resources/views/NewHome.blade.php) </code>
我的对应文件如下
Avatar.php
<code><?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Avatar extends Model {
public function article()
{
return $this -> hasMany('App\Article');
}
}
</code>Article.php
<code><?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
//
public function avatar()
{
return $this -> belongsTo('App\Avatar');
}
}
</code>Controller.php
<code> public function index()
{
$articles = Article::with('avatar')->latest()->paginate(8);
return view('NewHome', compact('articles'));
}
</code>NewHome.blade.php
<code> <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) }}">@@##@@avatars_id->url}}" alt="" width="180px"></a>
</div>
</div>
</div>
@endforeach
</div>
</div>
<div class="col-md-12">
{!!$articles->render()!!}
</div>
</code>article_table.php
<code> 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');
});
}
</code>avatars_table.php
<code> public function up()
{
Schema::create('avatars', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->string('url');
$table->text('path');
$table->timestamps();
});
}
</code>我不知道是哪里有问题,不管怎么弄都是报错..希望大神帮忙看看.谢谢了.
<code>php</code><code>//打印下你查询的SQL语句看看
public function article()
{
return $this -> hasMany('App\avatar','id','avatars_id');
}
public function avatar()
{
return $this -> belongsTo('App\App\Article','avatars_id','id');
}
</code>
你可以dd($articles)来看看,相信在relations这一个上面会是null,总感觉你的relationship声明好奇怪
看第15行输出的是哪个变量,然后dd打印变量,根据问题再逐步排解。
应该是你的调用关联用错了
<code>@@##@@avatars_id->url}}" alt="" width="180px"> </code>
这里应该用
<code>{{$page->avatar->url}}
</code>因为你这里用的是
<code>$articles = Article::with('avatar')->latest()->paginate(8);
</code>如果你要改变映射的名字,需要
<code>$articles = Article::with(array('avatars_id'=>function(){/*映射的关系*/}))->latest()->paginate(8);
</code>这之后你要改变很多
谢谢各位的答案,但我是最后自己解决的.
我的解决方案和我挑选的答案是一个方法.
我在Acticle.php里把内容改成了
<code> public function avatar()
{
return $this -> belongsTo('App\Avatar','avatars_id');
}
</code>就行了,关于关联到模板上面的内容是
{{$page->avatar->url}
模型 关联方法 对应字段
非常感谢各位,我也贴出正确解决我问题的方法.
希望可以给以后遇到此类问题的朋友一个解决方案.
其实你只要一路跟 convention 走乖乖地建表就用 avatar_id 就不会出这些问题了。
另外建议你 hasMany() 关系的时候也用复数
<code>public function articles()
{
return $this->hasMany('App\Article');
}
</code>这样写的时候自己也清楚了~
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号