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

前端 未结 15 2527
礼貌的吻别
礼貌的吻别 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:39

    For Laravel >= 5.2

    Use the ->pluck() method

    $roles = DB::table('roles')->pluck('title');
    

    If you would like to retrieve an array containing the values of a single column, you may use the pluck method


    For Laravel <= 5.1

    Use the ->lists() method

    $roles = DB::table('roles')->lists('title');
    

    This method will return an array of role titles. You may also specify a custom key column for the returned array:

提交回复
热议问题