Eloquent get only one column as an array

前端 未结 5 2069
时光说笑
时光说笑 2020-12-08 01:39

How to get only one column as one dimentional array in laravel 5.2 using eloquent?

I have tried:

$array = Word_relation::select(\'word_two\')->wh         


        
5条回答
  •  遥遥无期
    2020-12-08 02:14

    I came across this question and thought I would clarify that the lists() method of a eloquent builder object was depreciated in Laravel 5.2 and replaced with pluck().

    // <= Laravel 5.1
    Word_relation::where('word_one', $word_id)->lists('word_one')->toArray();
    // >= Laravel 5.2
    Word_relation::where('word_one', $word_id)->pluck('word_one')->toArray();
    

    These methods can also be called on a Collection for example

    // <= Laravel 5.1
      $collection = Word_relation::where('word_one', $word_id)->get();
      $array = $collection->lists('word_one');
    
    // >= Laravel 5.2
      $collection = Word_relation::where('word_one', $word_id)->get();
      $array = $collection->pluck('word_one');
    

提交回复
热议问题