2

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');

3 Answers 3

8

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');
Sign up to request clarification or add additional context in comments.

1 Comment

It is not true that there where clause can't accept an array.
2

You definitely can parse an array to the where method in Laravel Eloquent. This is directly referred to in the official documentation here.

Using syntax below certainly works:

$where=array();
$where[] = ['field_a', '=', 'a'];
$where[] = ['field_b', '=', 'test'];
$results = DB::table('your_table')->where($where)->get();

or

$where=array();
$where[] = ['field_a', '=', 'a'];
$where[] = ['field_b', '=', 'test'];
$results = YourModelName::where($where)->get();

Comments

0
$posts = Post::whereIn('collection_name', ["games", "sports", "science"])
             ->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.