简体   繁体   English

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

[英]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: 我会改变一些事情:

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

相关问题 Laravel属于关系 - 试图获得非对象的属性 - Laravel belongsTo relationship - Trying to get property of non-object 雄辩的关系返回错误试图在Laravel 5.4中获取非对象的属性 - Eloquent relationship returning error Trying to get property of non-object in Laravel 5.4 试图在 laravel eloquent 中获取非对象错误的属性“名称” - Trying to get property 'name' of non-object error in laravel eloquent Laravel 7 - 属于关系 - 试图获取非对象的属性 - Laravel 7 - belongsTo relation - Trying to get property of non-object Laravel Nova 3 - 资源字段错误 - BelongsTo - “试图获取非对象的属性‘resourceClass’” - Laravel Nova 3 - Resource Fields Error - BelongsTo - “Trying to get property 'resourceClass' of non-object” 雄辩的关系控制器投身于试图获取非对象的属性 - Eloquent Relationship Controller throw to Trying to get property of non-object Laravel Eloquent 关系返回试图获取非对象的属性“office_name” - Laravel Eloquent relationship return Trying to get property 'office_name' of non-object 尝试使用雄辩的关系时尝试获取非对象错误的属性 - Trying to get property of non-object error while trying to use eloquent relationship Laravel 4雄辩地试图获得非对象的属性 - Laravel 4 Eloquent Trying to get property of non-object Eloquent Laravel show试图获取非对象的属性“代码” - Eloquent Laravel show Trying to get property 'code' of non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM