[英]Laravel Eloquent : belongsTo relationship - Error: Trying to get property of non-object
First time to try laravel eloquent relatioinstip 第一次尝试laravel eloquent relatioinstip
I know it's really simple but I am getting this error don't know what's wrong with it 我知道这很简单但我得到这个错误不知道它有什么问题
I have 2 tables in data base, news and news_image 我在数据库,新闻和news_image中有2个表
in database 在数据库中
Tables: 表:
news
id | header | details
news_image
id | image | news_id
And have 2 models News , newsImage 并有2款新闻,newsImage
newsImage model : newsImage模型:
class newsImage extends Eloquant {
protected $table = 'news_image';
public function news()
{
return $this->belongsTo('News');
}
}
News model 新闻模式
class News extends Eloquent
{
protected $table = 'news';
public $timestamps = false;
public function image()
{
return $this->hasMany('newsImage');
}
}
The view: 风景:
foreach($news as $new)
<tr>
<td> {{$new->id}} </td>
<td> {{ $new->header}}</td>
<td> {{ $new->details }}</td>
</td> {{$new->news->image}}</td>
</tr>
when I run this it's get error : 当我运行它时会出现错误:
Trying to get property of non-object (View: /var/www/html/clinics/app/views/news/index.blade.php)
试图获取非对象的属性(查看:/var/www/html/clinics/app/views/news/index.blade.php)
Any ideas on what could be causing this error? 关于什么可能导致此错误的任何想法?
First, assuming what you are passing to your view is an array or Collection of News
objects, you should probably be using $new->image
to access the News Item
relation. 首先,假设您传递给视图的是数组或
News
集合对象,您可能应该使用$new->image
来访问News Item
关系。 By defining the function image()
in your News
model, you can access the relation with either the ->image
or ->image()
calls. 通过在
News
模型中定义函数image()
,您可以使用->image
或->image()
调用访问该关系。 In either case, what you need to call is probably 在任何一种情况下,您需要调用的可能是
$new->image->first()->image
To break that down: 打破这种情况:
->image
gets the Collection of NewsImage
relations ->image
获取NewsImage
关系的集合 ->first()
gets the first item in the Collection ->first()
获取Collection中的第一项 ->image
(the secone one) gets the image field from that NewsImage
->image
(secone one)从该NewsImage
获取图像字段 If the Collection has more than one item, you can instead loop over it to get all of the images as shown in the other answer. 如果Collection有多个项目,您可以循环遍历它以获取所有图像,如另一个答案所示。
There are a couple things I would change: 我会改变一些事情:
In your News model, change the relationship from "image" to "images" since it's a one to many relationship. 在您的新闻模型中,将关系从“图像”更改为“图像”,因为它是一对多的关系。 It just keeps your code clean.
它只是保持你的代码干净。
Your foreach loop in your view should loop through all the news models, but remember that each news model has multiple images, so you should have another loop inside your existing loop to display the images, ie foreach ($new->images as $image)
你的视图中的foreach循环应循环遍历所有新闻模型,但请记住每个新闻模型都有多个图像,因此你应该在现有循环中有另一个循环来显示图像,即
foreach ($new->images as $image)
@foreach ($news as $new) <tr> <td> {{$new->id}} </td> <td> {{ $new->header}}</td> <td> {{ $new->details }}</td> <td> @foreach ($new->images as $image) {{ $image->image }} @endforeach </td> </tr> @endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.