Laravel eloquent - 一对多关系(试图获取非对象的属性)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15693363/
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 07:39:31  来源:igfitidea点击:

Laravel eloquent - One to many relationships (Trying to get property of non-object)

phplaraveleloquent

提问by Ito Fachruzzaman

I have "posts" table that has many-to-one relationship with "categories" table. The goal is to show all of posts and their categories.

我有与“类别”表具有多对一关系的“帖子”表。目标是显示所有帖子及其类别。

Tables:
Posts: id, content, category_id, etc
Categories: id,name

表格:
帖子:id、内容、category_id 等
类别:id、名称

Here's my code

这是我的代码

Models:

楷模:

class Posts extends Eloquent
{
    public static $table = 'posts';

    public function categories()
    {
        return $this->belongs_to('Categories');
    }
}

class Categories extends Eloquent
{
    public static $table = 'categories';

    public function posts()
    {
        return $this->has_many('posts');
    }
}

My controller

我的控制器

public function get_posts()
{
    $posts = Posts::with('categories')->all();

    return View::make('admin.posts')
        ->with('title', 'Posts')
        ->with('posts', $posts);

}

My view

我的看法

@foreach($posts as $post)
        <tr>
            <td>{{ $post->title }}</td>
            <td>{{ $post->categories->name }}</td>
            <td><small> {{$post->updated_at}} </small></td>
            <td>
                <button>
                    {{HTML::link_to_route('edit_post','Edit',array($post->id))}}
                </button>
                {{Form::open('admin/delete','Delete')}}
                {{Form::hidden('id', $post->id)}}
                <input type="submit" name="edit_post" value="Delete"/>
                {{Form::close()}}
            </td>
        </tr>
@endforeach

ERROR:

错误:

Error rendering view: [admin.posts]
Trying to get property of non-object

I am a newbie, please help me solve this issues

我是新手,请帮我解决这个问题

回答by Bonci Marco

{{ $post->categories->name }} before test is categories exists

{{ $post->categories->name }} 在测试之前是类别存在

Example:

例子:

@if( ! empty($post->categories))
    <td>{{ $post->categories->name }}</td> 
@else

@end if

Aesis.

艾西斯。

回答by Can Geli?

Just use ::all()instead of ::with(..)->all()

只需使用::all()代替::with(..)->all()

回答by adib.mosharrof

categories is an array as you are using a has_many relationship. There are many categories, hence an array is returned, so to access it you have to index it like an array

category 是一个数组,因为您正在使用 has_many 关系。有很多类别,因此返回一个数组,因此要访问它,您必须像数组一样索引它

the correct solution would be

正确的解决方案是

$post->categories[0]->name

$post->categories[0]->name

回答by Xiao Liu

Are you using Laravel 4? First the syntax for declaring relationship is hasMany and belongsTo, in camel case. Check it out in Laravel documentation

你在使用 Laravel 4 吗?首先声明关系的语法是hasMany 和belongsTo,在驼峰情况下。在Laravel 文档中查看

In view, check if categories are empty collection, ie., whether post has its category:

在视图中,检查类别是否为空集合,即帖子是否有其类别:

@if($post->categories->count())
    <td>{{ $post->categories->name }}</td>
    ...
@endif

By the way, I would use singular form as model class name, like Post and Category instead of plural forms. And in Post class I would define inverse one-to-many relationship with singular form, to show there's only one entry in category table for this given Post.

顺便说一句,我会使用单数形式作为模型类名称,例如 Post 和 Category 而不是复数形式。在 Post 类中,我将定义具有单数形式的反向一对多关系,以显示此给定 Post 的类别表中只有一个条目。

public function category()
    {
        return $this->belongsTo('Category');
    }