如何雄辩地将select的输出转换为list?

问题描述 投票:0回答:1

在以下查询中:

$search = 'boo';
MyModel::select('id', 'title')->where('title', 'like', '%' . $search . '%')->get();

我想获得以下输出:

[
    1 => 'book',
    4 => 'booth'
]

我尝试了以下操作,但没有成功:

$search = 'boo';
MyModel::lists('id', 'title')->where('title', 'like', '%' . $search . '%')->get();
php laravel eloquent
1个回答
0
投票

您正在寻找pluck()功能。尝试:MyModel::where('title', 'like', '%' . $search . '%')->pluck('title', 'id');

https://laravel.com/docs/7.x/queries#retrieving-results上的更多详细信息>

© www.soinside.com 2019 - 2024. All rights reserved.