Laravel 5.2 - pluck() method returns array

前端 未结 5 733
悲&欢浪女
悲&欢浪女 2020-12-04 21:13

I\'m trying to upgrade my project L5.1 -> L5.2. In upgrade guide there\'s one thing which isn\'t clear for me:

The lists method on the Co

5条回答
  •  一整个雨季
    2020-12-04 22:10

    laravel pluck returns an array

    if your query is:

     $name = DB::table('users')->where('name', 'John')->pluck('name');
    

    then the array is like this (key is the index of the item. auto incremented value):

    [
        1 => "name1",
        2 => "name2",
        .
        .
        .
        100 => "name100"
    ]
    

    but if you do like this:

    $name = DB::table('users')->where('name', 'John')->pluck('name','id');
    

    then the key is actual index in the database.

    key||value
    [
        1 => "name1",
        2 => "name2",
        .
        .
        .
        100 => "name100"
    ]
    

    you can set any value as key.

提交回复
热议问题