0

I use this query and i get a error :

 $description = $request->get('description');
        if (!empty($description)){
        $description_query = Transcationhistorique::where(['sender_id' => $user_id, "%$description%", 'LIKE','description'])
            ->orWhere('receiver_id', $user_id)->get();
        }else{
            $description_query  =  "" ;
        }

and this is the error that I get :

"SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from transcation_historique where (sender_id = 32 and 0 = %salaire% and 1 = LIKE and 2 = description) or receiver_id = 32)"

and this what really i want to run:

select * from `transcation_historique` where (`sender_id` = 32 and `description` = %salaire%) or `receiver_id` = 32)
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
Ahmed Guesmi
  • 350
  • 7
  • 26

4 Answers4

3

Try this,

$description_query = Transcationhistorique::where(
                         function($query) use ($user_id, $description){
                             return $query->where('sender_id', $user_id)
                                          ->where('description', 'like', '%' . $description .'%');

                         }
                     )
                     ->orWhere('receiver_id', $user_id)
                     ->get();
Abhay Maurya
  • 11,819
  • 8
  • 46
  • 64
Jitendra
  • 584
  • 1
  • 10
  • 28
1

It seems like your where query is not structured correctly. You should use the following structure if you want to use operators other than "=" Source

$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])

My suggestion is:

 $description = $request->get('description');
 if (!empty($description)){
     $description_query = Transcationhistorique::where([
            ['sender_id', '=', $user_id],
            ['description', 'LIKE', "%{$description}%"]
     ])
     ->orWhere('receiver_id', $user_id)->get();
 }else{
     $description_query  =  "" ;
 }

1

Try this one:

Transcationhistorique::where('receiver_id', '=', $user_id)
        ->orWhere(function ($query) use ($user_id, $description) {
            $query->where('sender_id', '=', $user_id)->where('description', 'LIKE', "%".$description."%");
        })->get();
dparoli
  • 8,891
  • 1
  • 30
  • 38
0

You can use multiple where method as and. Can you try following codes?

    $description = $request->get('description');
    if (!empty($description)){
        $description_query = Transcationhistorique::where('sender_id', $user_id)
        ->where("description", 'LIKE','%' . $description . '%')
        ->orWhere('receiver_id', $user_id)->get();
    }else{
        $description_query  =  "" ;
    }
FGDeveloper
  • 1,030
  • 9
  • 23