在以下查询中:
$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
关于php - 如何将 select 的输出转换为 Eloquent 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61350596/