skip to Main Content

I just want to make a where clause from string as my where clause depends upon the other variable. I did like this

$where_condition = "post_flight_informations.schedule_year,=,2022";

and then applied to query like this

->where($where_condition)

but it is taking $where_condition as a single argument so i have a Missing argument 2 error can someone help me on this thank you

5

Answers


  1. Chosen as BEST ANSWER

    Thank you all finally i solved it like this

    $where_condition = ["post_flight_informations.schedule_year", "=" , $request_year];
    

    and

    ->where(...$where_condition)
    

  2. Well in that case, you should make your variable to be like this :

    $where_condition = [
        ['post_flight_informations.schedule_year', '=', 2022],
    ];
    

    and the apply your query

    ->where($where_condition)->get();
    
    Login or Signup to reply.
  3. where condition used two arguments.

    $where_condition = "post_flight_informations.schedule_year,=,2022";
    
    // Split the condition into an array
    $condition_parts = explode(',', $where_condition);
    
    //Pass the parts as individual arguments
    $query->where(...$condition_parts);
    
    Login or Signup to reply.
  4. On the laravel docs you should put a 3 arguments in where method to query.

    In your question you are passing a single argument. This is the correct way to use where method to add where in to your query.

    ->where("post_flight_informations.schedule_year","=","2022")->get();
    

    The first argument is name of the column. Second argument is operator. Third is value to compare in the column value.

    Login or Signup to reply.
  5. I would suggest you to use whereRaw
    like the following example

    $whereRawStmt = 'post_flight_informations.schedule_year = 2022';
    

    then use this variable with

    ->whereRaw($whereRawStmt)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search