In Laravel database can I use a previously made array of conditions in where function?

  laravel, mysql, rdbms

In laravel tutorial it’s said that we can pass an array of conditions in where function. My question is when I pass an array of conditions previously made by me it doesn’t work.

Here is the code in documentation:

$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();

My code is:

$fields = collect($request->input());
    if($fields->has('data')){
        $fields = collect($fields->get('data'));
        $conditions = collect([]);
        if($fields->has('likes')){
            $fieldsL = $fields->get('likes');
            $conditions->push(['likes', $fieldsL]);
        }
        if($fields->has('title')){
            $fieldsT = $fields->get('title');
            $conditions->push(['title', $fieldsT]);
        }
        $data = DB::table('posts')->select('*')->where($conditions)-

>get();
        }

My error message:

IlluminateDatabaseQueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column '[]' in 'where clause' (SQL: select * from `posts` where `[]` is null) in file D:laravellara-apivendorlaravelframeworksrcIlluminateDatabaseConnection.php on line 68

PLEASE let me know if it’s impossible.
thanks.

Source: Laravel

Leave a Reply