简体   繁体   English

Laravel 4 Eloquent的关系无法正常工作

[英]Relation of Laravel 4 Eloquent not working

I am setting up two Models. 我正在设置两个模型。 Let's assume we have a portfolio and portfolio_category table 假设我们有一个Portfolio和Portfolio_category表

Table portfolio 表组合

id
cat_id
name
status

Table portfolio_category 表Portfolio_category

cat_id
cat_name

Model 模型

class Portfolio extends \Eloquent {
    protected $fillable = [];   
    public function portfolio_category() 
    {
        return $this->hasMany('Portfolio_category');
    }
}

class Portfolio_category extends \Eloquent {
    protected $fillable = [];
    public function portfolio() 
    {
        return $this->belongsTo('Portfolio');
    }
}

My code: 我的代码:

$data = Portfolio::all();
foreach($data as $value){
    $id = $value['id'];
    $name = $value['name'];
    $cat_name = $value['cat_name'];
}

I want to display category name but it shows null values. 我想显示类别名称,但它显示空值。

I am pretty confused, what went wrong? 我很困惑,怎么了? any idea!!! 任何想法!!!

I got my answer 我得到了答案

$data = Portfolio::join('portfolio_category', 'portfolio_category.cat_id', '=', 'portfolio.cat_id')->get();
foreach($data as $value){
    $id = $value['id'];
    $name = $value['name'];
    $cat_name = $value['cat_name'];
}

Your relationships are not defined correctly. 您的人际关系定义不正确。 In your code, you've tried to define the Portfolio as the parent, and the Portfolio_category as the child. 在您的代码中,您尝试将Portfolio定义为父项,并将Portfolio_category定义为子项。 However, your tables are setup such that the Portfolio_category is the parent and the Portfolio is the child. 但是,您的表被设置为Portfolio_category是父级,而Portfolio是子级。 Basically, the table that contains the foreign key (portfolio table has foreign key to portfolio_category) should be on the belongsTo side. 基本上,包含外键的表(组合表具有对Portfolio_category的外键)应该在belongsTo端。

So, instead of portfolio hasMany portfolio_category and portfolio_category belongsTo portfolio, it needs to be switched to portfolio_category hasMany portfolio and portfolio belongsTo portfolio_category. 因此,与其将hasMany Portfolio_category和Portfolio_category属于toy Portfolio转换为投资组合,还需要将其切换到hasMany投资组合和togostayTo Portfolio_category的投资组合_category。

Additionally, I'm assuming these were removed for brevity, but the table names and the primary key on your portfolio_category table do not conform to Laravel conventions, so you need to specify these properties in the model. 另外,为简便起见,我假设将它们删除了,但是Portfolio_category表上的表名和主键不符合Laravel约定,因此您需要在模型中指定这些属性。

Your models should look like: 您的模型应如下所示:

class Portfolio extends \Eloquent {
    protected $table = 'portfolio';
    protected $fillable = [];

    public function portfolio_category()
    {
        return $this->belongsTo('Portfolio_category');
    }
}

class Portfolio_category extends \Eloquent {
    protected $table = 'portfolio_category';
    protected $primaryKey = 'cat_id';
    protected $fillable = [];

    public function portfolios() 
    {
        return $this->hasMany('Portfolio');
    }
}

Now you can access the related tables through the relationships: 现在,您可以通过关系访问相关表:

$data = Portfolio::with('portfolio_category')->get();
foreach($data as $value){
    $id = $value->id;
    $name = $value->name;
    $cat_name = $value->portfolio_category->cat_name;
}

For more on the Eloquent ORM, the documentation can be found here (L4.2) or here (L5.0) . 有关Eloquent ORM的更多信息,可在此处(L4.2)此处(L5.0)找到文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM