Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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 5返回JSON而不是belongsTo()关系上的对象_Php_Laravel_Laravel 5_Laravel 5.3 - Fatal编程技术网

Php Laravel 5返回JSON而不是belongsTo()关系上的对象

Php Laravel 5返回JSON而不是belongsTo()关系上的对象,php,laravel,laravel-5,laravel-5.3,Php,Laravel,Laravel 5,Laravel 5.3,我已经安装了Laravel framework v5.3.2和软件包v6.0.1。我在从同一模型上的belongsTo()关系(父)获取数据时遇到问题 Category.php模型 class Category extends Eloquent { public $translatedAttributes = [ 'name', 'slug', 'description' ]; public function category() {

我已经安装了Laravel framework v5.3.2和软件包v6.0.1。我在从同一模型上的belongsTo()关系(父)获取数据时遇到问题

Category.php模型

class Category extends Eloquent
{
    public $translatedAttributes = [
        'name', 'slug', 'description'
    ];

    public function category()
    {
        return $this->belongsTo('App\Model\Category', 'category_id', 'id');
    }

    public function categories()
    {
        return $this->hasMany('App\Model\Category', 'category_id', 'id');
    }
}
获取所有类别列表的方法:

$categoryModel = new Category;
$categories = $categoryModel->with('category.translations')->get();
当我在视图中打印name属性时,Laravel抛出异常:“试图获取非对象的属性”

foreach内部查看的dd($category)结果:

Category {#231 ▼
    #table: "category"
    +translatedAttributes: array:4 [▶]
    +timestamps: false
    #connection: null
    #primaryKey: "id"
    #keyType: "int"
    #perPage: 15
    +incrementing: true
    #attributes: array:3 [▶]
    #original: array:3 [▶]
    #relations: array:2 [▼
        "category" => Category {#220 ▶}
        "translations" => Collection {#228 ▶}
    ]
    #hidden: []
    #visible: []
    #appends: []
    #fillable: []
    #guarded: array:1 [▶]
    #dates: []
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #with: []
    +exists: true
    +wasRecentlyCreated: false
}

因此,对象存在,但当我尝试直接访问属性时,Laravel没有正确显示它。有人知道问题出在哪里吗?它是在Laravel中还是在Laravel可翻译包中?

此代码返回对象集合:

$categoryModel->with('category')->get();
但是你试图把它当作对象,这就是为什么你会出错

您需要迭代集合以使用其中的对象,因此请尝试以下操作:

@foreach ($categories as $category)
    {{ $category->category->name }}
@endforeach

此代码返回对象的集合:

$categoryModel->with('category')->get();
但是你试图把它当作对象,这就是为什么你会出错

您需要迭代集合以使用其中的对象,因此请尝试以下操作:

@foreach ($categories as $category)
    {{ $category->category->name }}
@endforeach

好的,请用foreach循环内的
{{dd($category)}
结果更新您的问题(或在某处发布esle)。更新的问题。嗯,奇怪,类别关系是空的。为什么我要急于加载父类别?也许集合中的第一个类别没有任何类别或其他东西?哈哈,这是真的;)将类别的getter编辑为:$categories=$this->categoryModel->with('category.translations')->get();获取父类别的翻译。但仍然不知道为什么它不起作用。dd()或debug()树没问题,我看到了category->relations->category->relations->translations…我明白了,你的第二条评论回答了它:“也许集合中的第一个类别没有任何类别或其他东西?”。所以这是有效的-谢谢!好的,请用foreach循环内的
{{dd($category)}
结果更新您的问题(或在某处发布esle)。更新的问题。嗯,奇怪,类别关系是空的。为什么我要急于加载父类别?也许集合中的第一个类别没有任何类别或其他东西?哈哈,这是真的;)将类别的getter编辑为:$categories=$this->categoryModel->with('category.translations')->get();获取父类别的翻译。但仍然不知道为什么它不起作用。dd()或debug()树没问题,我看到了category->relations->category->relations->translations…我明白了,你的第二条评论回答了它:“也许集合中的第一个类别没有任何类别或其他东西?”。所以这是有效的-谢谢!
@foreach ($categories as $category)
    {{ $category->category->name }}
@endforeach