访问 Eloquent 个关系属性

Accessing Eloquent relationship attributes

我有三个模型,它们都是一对多关联的。类别、子类别和样式。我有双向工作的关系 - 尽管我似乎在访问相关属性时遇到问题。

在我的查询有 运行 之后,我剩下一个 Style 实例,其中 'relations' 是 Subcategory 的一个实例,而 Subcategory 中的 'relations' 是一个 Category 的实例.都是正确的。

问题是我现在似乎无法访问相关的模型实例。例如,如果我调用:

$style->subcategory->name;

我得到 'Trying to get property of non-object'。所以我尝试只调用 $style->subcategory,结果是“1”。

为什么 $style->subcategory return 不是子类别模型的实例?我是不是遗漏了什么或者我的理解有误?

--编辑--

型号

类别

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Category extends Eloquent {

    protected $table = 'product_categories';

    protected $fillable = [
            'name',
            'slug',
            'image'
    ];

    public function subcategories() {
            return $this->hasMany('Paragon\Products\Subcategory', 'category');
    }

}

子类别

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

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

}

风格

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Style extends Eloquent {

    protected $table = 'product_styles';

    protected $fillable = [
            'subcategory',
            'name',
            'slug',
            'image'
    ];

    public function subcategory() {
            return $this->belongsTo('Paragon\Products\Subcategory', 'subcategory');
    }

}

查询

$style->where($id, $item)->with('subcategory.category')->first();

表格

Paragon\Products\Category

ID    ...
1
2

Paragon\Products\Subcategory

ID   Category    ...
1    2
2    2

Paragon\Products\Style

ID   Subcategory    ...
1    1
2    1

由于 Style 模型中的 subcategory 方法应该引用 Subcategory 的单个实例而不是它们的集合,我难道不能按照我现在(或正在尝试)的方式调用属性吗?

我这里是在黑暗中拍摄的。但我会尝试解释如何使用集合以及 first 和 get 之间的区别。

$users= $user-> with('images') -> first(); <-- this is first  row in the table users, each user has many images.
$users-> username; //works;

$users-> images-> image_name; // wont work , model has many ,    
you get error <-- Trying to get property of non-object. 
// access the proprety image and loop through the collection of objects.
$images = $user-> images; // 

foreach ($images as $image){
  echo $image- >image_name; 
}

另一方面,如果图像确实属于一个用户并且该用户有一个图像。您可以像这样访问 image_name

$user -> image(() -> image_name;

你的情况

$style -> subcategory() -> name;

通过带有子类别的 id 获取样式

$style -> with('subcategry') -> where('id', $styleId) -> first();

好的,我想我现在明白发生了什么事了。您的 Eloquent 模型称为子类别,但外键也是。所以当你打电话给

$style->subcategory

那就是返回外键而不是模型。要解决此问题,我建议将外键 ID 的名称更改为 subcategory_id。如果你不能改变数据库,你可以通过将方法与这样的东西链接起来来强制它使用模型

$style->subcategory()->first()->name

编辑: 另一个想法,您可以将关系的名称更改为

public function subcategory_item()
{
  return $this->belongsTo('Paragon\Products\Subcategory', 'subcategory');
}

那么你应该能够正确引用它

$style->subcategory_item->name