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) 

4 Answers 4

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

3 Comments

i always get all transaction
Are you sure that you're getting a not empty description?
I think for all the cases this condition is excuting and you are getting all transaction. ->orWhere('receiver_id', $user_id)
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 Comment

i always get all transaction
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(); 

Comments

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 = "" ; } 

6 Comments

Look at his manul query. where( multiple condition ) or where( single condition)
i always get all transaction
I can't understand you? I said that where "method can use much times".
Just i don't get the empty description rows but i get all my rows
If you want using two and one or criteria on your query. My solution will work.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.