php 访问 Eloquent 关系属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32254259/
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
Accessing Eloquent relationship attributes
提问by Mike Kaperys
I have three models all related by one-to-many. Category, Subcategory and Style. I have relationships working both ways - though I seem to have a problem accessing related attributes.
我有三个模型都是一对多的。类别、子类别和样式。我有双向工作的关系 - 尽管我似乎在访问相关属性时遇到问题。
After my queries have ran, I'm left with this an instance of Style where 'relations' is an instance of Subcategory, and 'relations' in Subcategory is an instance of Category. Which is all correct.
在我的查询运行后,我留下了一个 Style 实例,其中“关系”是子类别的一个实例,而子类别中的“关系”是类别的一个实例。这是正确的。
The problem is that I now seem to not be able to access the related model instances. For example, if I call:
问题是我现在似乎无法访问相关的模型实例。例如,如果我打电话:
$style->subcategory->name;
I get 'Trying to get property of non-object'. So I tried calling just $style->subcategory and the result is '1'.
我得到'试图获得非对象的财产'。所以我尝试只调用 $style->subcategory,结果是“1”。
Why doesn't $style->subcategory return the instance of the subcategory model? Am I missing something or is my understanding incorrect?
为什么 $style->subcategory 不返回子类别模型的实例?我错过了什么还是我的理解不正确?
--EDIT--
- 编辑 -
Models
楷模
Category
类别
<?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');
}
}
Subcategory
子类
<?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');
}
}
Style
风格
<?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');
}
}
Query
询问
$style->where($id, $item)->with('subcategory.category')->first();
Tables
表
Paragon\Products\Category
Paragon\产品\类别
ID ...
1
2
Paragon\Products\Subcategory
Paragon\产品\子类别
ID Category ...
1 2
2 2
Paragon\Products\Style
Paragon\产品\风格
ID Subcategory ...
1 1
2 1
Since the subcategory method in the Style model should refer to a single instance of Subcategory and not a Collection of them, shouldn't I be able to just call attributes the way I am (or am trying to)?
由于 Style 模型中的 subcategory 方法应该引用 Subcategory 的单个实例而不是它们的 Collection,所以我不应该能够按照我(或正在尝试)的方式调用属性吗?
回答by Adrian Flannery
Ok I think I see now what is going on. Your Eloquent model is called subcategory, but so is the foreign key. So when you call
好的,我想我现在明白发生了什么。您的 Eloquent 模型称为子类别,但外键也是。所以当你打电话
$style->subcategory
That is returning the foreign key instead of the model. To fix this, I'd recommend changing the name of the foreign key id to subcategory_id. If you can't change the database, you could force it to use the model by chaining the method with something like this
那是返回外键而不是模型。要解决此问题,我建议将外键 ID 的名称更改为 subcategory_id。如果您无法更改数据库,则可以通过将方法与类似的内容链接来强制它使用该模型
$style->subcategory()->first()->name
Edit: Another idea, you could change the name of the relationship to something like
编辑:另一个想法,您可以将关系的名称更改为类似
public function subcategory_item()
{
return $this->belongsTo('Paragon\Products\Subcategory', 'subcategory');
}
Then you ought to be able to properly reference it with
那么你应该能够正确地引用它
$style->subcategory_item->name
回答by mdamia
I am taking a shot in the dark here. But I will try and explain how to work with collection and difference between first and get.
我在这里在黑暗中拍摄。但我将尝试解释如何处理收集以及 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;
}
on the other hand if the image did belong to one user and the user has one image. You can access image_name like this
另一方面,如果图像确实属于一个用户并且用户有一个图像。您可以像这样访问 image_name
$user -> image(() -> image_name;
in your case
在你的情况下
$style -> subcategory() -> name;
to get a style by id with subcategoty
通过带有子类别的 id 获取样式
$style -> with('subcategry') -> where('id', $styleId) -> first();