Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩:belongsTo关系-错误:尝试获取非对象的属性_Php_Laravel - Fatal编程技术网

Php Laravel雄辩:belongsTo关系-错误:尝试获取非对象的属性

Php Laravel雄辩:belongsTo关系-错误:尝试获取非对象的属性,php,laravel,Php,Laravel,第一次尝试拉威尔雄辩的关系 我知道这很简单,但我得到了这个错误,我不知道这是怎么回事 我有两个数据库表,新闻和新闻图片 在数据库中 class newsImage extends Eloquant { protected $table = 'news_image'; public function news() { return $this->belongsTo('News'); } } class News extends E

第一次尝试拉威尔雄辩的关系

我知道这很简单,但我得到了这个错误,我不知道这是怎么回事

我有两个数据库表,新闻和新闻图片

在数据库中

 class newsImage extends Eloquant {

    protected $table = 'news_image';
    public function news()
    {
        return $this->belongsTo('News');
    }    
}
class News extends Eloquent 
{

    protected $table = 'news';

    public $timestamps = false;


    public function image()
    {
        return $this->hasMany('newsImage');
    }


}
表:

news  
id | header | details 

news_image
id | image | news_id 
并有两个模型新闻,新闻图片

新闻图像模型:

 class newsImage extends Eloquant {

    protected $table = 'news_image';
    public function news()
    {
        return $this->belongsTo('News');
    }    
}
class News extends Eloquent 
{

    protected $table = 'news';

    public $timestamps = false;


    public function image()
    {
        return $this->hasMany('newsImage');
    }


}
新闻模式

 class newsImage extends Eloquant {

    protected $table = 'news_image';
    public function news()
    {
        return $this->belongsTo('News');
    }    
}
class News extends Eloquent 
{

    protected $table = 'news';

    public $timestamps = false;


    public function image()
    {
        return $this->hasMany('newsImage');
    }


}
观点:

foreach($news as $new)
<tr>
   <td> {{$new->id}} </td>
   <td> {{ $new->header}}</td>
   <td> {{ $new->details }}</td>
   </td> {{$new->news->image}}</td>
</tr>
foreach($news as$new)
{{$new->id}
{{$new->header}
{{$new->details}
{{$new->news->image}
当我运行此命令时,出现get错误:

正在尝试获取非对象的属性(视图:/var/www/html/clinics/app/views/news/index.blade.php)


你知道是什么导致了这个错误吗?

有几件事我会改变:

  • 在新闻模型中,将关系从“图像”更改为“图像”,因为它是一对多关系。它只是让你的代码保持干净

  • 视图中的foreach循环应该在所有新闻模型中循环,但请记住,每个新闻模型都有多个图像,因此在现有循环中应该有另一个循环来显示图像,即
    foreach($new->images as$image)

    @foreach($news as$new)
    {{$new->id}
    {{$new->header}
    {{$new->details}
    @foreach($new->image as$image)
    {{$image->image}
    @endforeach
    @endforeach
    

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

    $new->image->first()->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
    
    要分解这一点:

    • ->image
      获取
      新闻图像
      关系的集合
    • ->first()
      获取集合中的第一项
    • ->image
      (secone)从该
      新闻图像

    如果集合有多个项目,您可以将其循环以获得其他答案中所示的所有图像。

    您也可以发布您的新闻模型吗?更新并发布新闻模型假设这是复制/粘贴的代码,则您的
    newsImage
    类不会扩展
    Eloquant
    这样的东西。但我假设,既然你没有从中得到错误,这不是复制/粘贴?从什么中得到错误?$news=news::paginate(15);这就是我要表达的观点谢谢你的帮助我明白了:)