调用数组 laravel 5.0.35 上的成员函数 where()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40891099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function where() on array laravel 5.0.35
提问by Viktor
I`m use
我在用
public function getImages($array_symbols_id){
$array_images = DB::table('photo')
->whereIn('photo_symbol_id', $array_symbols_id)
->where('photo_moderation_id','2')
->orderByRaw('RAND()')
->get(['photo_id', 'photo_src', 'photo_symbol_id']);
then try
然后尝试
$array = array();
foreach ($array_symbols_id as $id) {
$array[] = $array_images->where('photo_symbol_id', $id)->first()->photo_src;
}
but i have exception Call to a member function where() on array.
但我对数组上的成员函数 where() 有异常调用。
Why DB::table returns an array but not a collection?
为什么 DB::table 返回一个数组而不是一个集合?
laravel v5.0.35
Laravel v5.0.35
回答by Antonio Carlos Ribeiro
In Laravel 5.0, DB returns an array and Model returns collections, so you can make it a collection by using collect()
:
在 Laravel 5.0 中,DB 返回一个数组,而 Model 返回一个集合,因此您可以使用collect()
以下命令使其成为一个集合:
$array_images = collect(DB::table('photo')
->whereIn('photo_symbol_id', $array_symbols_id)
->where('photo_moderation_id','2')
->orderByRaw('RAND()')
->get(['photo_id', 'photo_src', 'photo_symbol_id']));