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

15,310

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']));
Share:
15,310

Related videos on Youtube

Viktor
Author by

Viktor

PHP 6+ years, Golang 1+ year, JavaScript 3+ years (native, vue, react). Highly familiar with a big number of web programming tools. Diploma in Computer Science

Updated on June 04, 2022

Comments

  • Viktor
    Viktor almost 2 years

    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