skip to Main Content

form models


    {

        //filters search

        if(isset($filters['search']) ? $filters['search'] : false){

            return $query->where('tittle','like','%' . $filters('search') . '%')

            ->orWhere('body', 'like', '%' .$filters('search') . '%');

        }

    }

and from controller

            "posts" => Post::latest()->filter(request(['search']))->get()

and i get Array callback must have exactly two elements

i just learning laravel 8 please help me

3

Answers


  1. Chosen as BEST ANSWER

    just change it $filters['search'] not $filters('search')


  2. You need to fix your conditional check inside the if block, you are doing extra work there. Rather than using isset you can use empty This will check if the key exists and it is not null and empty too. So it will be like:

         if (!empty($filters['search'])) {
            return $query->where('tittle', 'like', '%'.$filters['search'].'%')
                ->orWhere('body', 'like', '%'.$filters['search'].'%');
        }
    

    Also, if you are trying to fetch search request from the request then you should just use request('search). And in the filter method (scope). Are you sure that you need array there since you are passing a single search value, is request('search) an array?

    Login or Signup to reply.
  3. $search = data_get($filters, 'search', false);
    return $query->when(!empty($search), function ($query) use ($search) {
        $query->where('tittle', 'like', "%$search%")
            ->orWhere('body', 'like', "%$search%");
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search