Laravel Eloquent:belongsTo 关系 - 错误:试图获取非对象的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30943344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:48:47  来源:igfitidea点击:

Laravel Eloquent : belongsTo relationship - Error: Trying to get property of non-object

phplaravel

提问by Muhammed

First time to try laravel eloquent relatioinstip

第一次尝试 laravel eloquentrelationinstip

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

我在数据库中有 2 个表,news 和 news_image

in database

在数据库中

Tables:

表格:

news  
id | header | details 

news_image
id | image | news_id 

And have 2 models News , newsImage

并有 2 个模型 News , newsImage

newsImage model :

新闻图片模型:

 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?

关于可能导致此错误的任何想法?

采纳答案by BakerStreetSystems

There are a couple things I would change:

有几件事我会改变:

  1. 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.

  2. 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, i.e. 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
    
  1. 在您的新闻模型中,将关系从“图像”更改为“图像”,因为它是一对多的关系。它只是让你的代码保持干净。

  2. 您视图中的 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
    

回答by wunch

First, assuming what you are passing to your view is an array or Collection of Newsobjects, you should probably be using $new->imageto access the News Itemrelation. By defining the function image()in your Newsmodel, you can access the relation with either the ->imageor ->image()calls. In either case, what you need to call is probably

首先,假设您传递给视图的是一个数组或News对象集合,您可能应该使用它$new->image来访问News Item关系。通过image()News模型中定义函数,您可以使用->image->image()调用访问关系。在任何一种情况下,您需要调用的可能是

$new->image->first()->image

$new->image->first()->image

To break that down:

分解一下:

  • ->imagegets the Collection of NewsImagerelations
  • ->first()gets the first item in the Collection
  • ->image(the secone one) gets the image field from that NewsImage
  • ->image获取NewsImage关系集合
  • ->first()获取集合中的第一项
  • ->image(第二个)从中获取图像字段 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 有多个项目,您可以改为循环遍历它以获取其他答案中显示的所有图像。