Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function where() on array laravel 5.0.35

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.

Why DB::table returns an array but not a collection?

laravel v5.0.35

like image 548
Viktor Avatar asked Nov 30 '16 14:11

Viktor


1 Answers

In Laravel 5.0, DB returns an array and Model returns collections, so you can make it a collection by using 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']));
like image 174
Antonio Carlos Ribeiro Avatar answered Sep 28 '22 23:09

Antonio Carlos Ribeiro