简体   繁体   中英

Relation of Laravel 4 Eloquent not working

I am setting up two Models. Let's assume we have a portfolio and portfolio_category table

Table portfolio

id
cat_id
name
status

Table 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. However, your tables are setup such that the Portfolio_category is the parent and the Portfolio is the child. Basically, the table that contains the foreign key (portfolio table has foreign key to portfolio_category) should be on the belongsTo side.

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.

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.

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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