Search code examples
arrayslaraveleloquentwhere-clause

Laravel - Eloquent where with array


I'm trying to execute this query according to the documentation but its returning the error array_key_exists(): The first argument should be either a string or an integer

https://laravel.com/docs/5.8/queries#where-clauses

 $imagessize = $galleryObj->photos->where([
                ['gallery_id','=', $gallery->id],
                ['delete','=',0],
            ])->sum('filesize');

Solution

  • The where method does not accept arrays, if you want that you should use the whereIn method:

    The whereIn method verifies that a given column's value is contained within the given array:

    For example:

    users = DB::table('users')
                    ->whereIn('id', [1, 2, 3])
                    ->get();
    

    This will return the users with id 1,2 and 3.

    But in your case i dont think you need that. Try this instead:

    $imagessize = $galleryObj->photos->where('gallery_id',$gallery->id)->where('delete', 0)->sum('filesize');