简体   繁体   中英

Get other table column value in laravel 5.4

Hello i have below query

Deal::with(array('deal_category'=>function($query){
                    $query->select('name as dealcategory');
                }))->get()

when i try to retrieve dealcategory it does not return any value. I have defined relationship in model like

Deal Model

public function deal_category()
{
    return $this->belongsTo('App\DealCategory', 'deal_category_id', 'id');
}

And Deal category model like

public function deals(){
    return $this->hasMany('App\Deal','deal_category_id');
}

can anyone help how can i get categoryname?

You have to select the primary key to retrieve the necessary results.

Deal::with(array('deal_category'=>function($query){
                $query->select('id', 'name as dealcategory');
            }))->get()

Use facade DB . You can try something like this:

DB::table('deal as d')
    ->join('deal_category as dc', 'd.id', '=', 'dc.deal_id')
    ->select('d.name as dealname', 'dc.categoryname')
    ->get();

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