Laravel Eloquent: How to get only certain columns from joined tables

前端 未结 15 2484
礼貌的吻别
礼貌的吻别 2020-12-02 07:57

I have got 2 joined tables in Eloquent namely themes and users.

theme model:

public function user() {
  return $this->belongs_to(         


        
15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 08:30

    You can supply an array of fields in the get parameter like so:

    return Response::eloquent(Theme::with('user')->get(array('user.username'));
    

    UPDATE (for Laravel 5.2) From the docs, you can do this:

    $response = DB::table('themes')
        ->select('themes.*', 'users.username')
        ->join('users', 'users.id', '=', 'themes.user_id')
        ->get();
    

提交回复
热议问题