在以下查询中:
$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();
您正在寻找pluck()
功能。尝试:MyModel::where('title', 'like', '%' . $search . '%')->pluck('title', 'id');
https://laravel.com/docs/7.x/queries#retrieving-results上的更多详细信息>