Why, request return symbol — '?' - laravel

Why any value in a variable $search return a character — ??
$search = 'words'
Post::where("description", 'like', "%".$search."%")->toSql();
Result:
"select * from `post` where `description` like ?"

The method toSql() does not include bindings, and therefore you're seeing the question mark in their place. To see the bindings, you can use getBindings() like so:
$search = 'words';
Post::where("description", 'like', "%".$search."%")->getBindings();

Related

A way to determine if "where like" matches 100% with a post in Laravel?

Is there a way to determine if this query:
$news_result = Post::with('image')
->has('image')
->with('tags')
->where('title', 'LIKE', '%' . $search . '%')
->get();
Has a 100% match with some title? If a user searches for "Some random news title" and in the database there is a post with such title to return some kind of marker?
The LIKE operator is used to partially match something.
You should not use it, and do ->where('title', $search) instead.
Remove '%' in your code
$news_result = Post::with('image')->has('image')->with('tags')->where('title', $search)->get();

How i get column while compare json encoded data in select query?

The users is json encoded
$agent_id = $request->get('user');
$model = AgentPermission::select('module_category')->where('users', 'like', '%$agent_id%')->get();
I want to to show "module category" where json encoded data is present in "users" column
if it is working the whole code is working fine
because when i use this
$model = AgentPermission::select('module_category')->where('users', 'like', '%"10"%')->get(); it is working perfect
The comparison should be inside double quotes, not single quotes
$model = AgentPermission::select('module_category')->where('users', 'like', "%$agent_id%")->get();
since PHP injects variables only when using double quotes.
The alternative is:
$model = AgentPermission::select('module_category')->where('users', 'like', '%' . $agent_id . '%')->get();

Laravel Eloquent Where Query with multiple words

I have the following query (cut for brevity):
$employees = Employee::where('first_name', 'LIKE', "%$query%")
->orWhere('last_name', 'LIKE', "%$query%")
Now this works when the user inputs a single name like 'John' or 'Smith', but when they input 'John Smith' it doesn't find anything. Am I missing an extra orWhere?
Try this :
Employee::where(DB::raw('CONCAT(first_name," ",lastname)'), 'LIKE', "%' . $query . '%"))
You would have to add a 3rd orWhere. For our search function we use something like this:
Employee::whereraw("COALESCE(last_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(first_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(last_name + ', ' + first_name, '') LIKE '%$query%'")
Adding Coalesce seemed to help with some issues we had when we first implemented it, not sure if it is necessary in your case though.
You can do :
$fullName = trim($query);
$employees = Employee::where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'LIKE', "%".$fullName."%")->get();
You are concatenating the values in database for first_name + ' ' + last_name and then using like to find the matching records.

query returning wrong result for like'%'

like % not working . the query returning result for full string match not the substring match.
Booking::whereHas('agent', function ($query) use ($agent_name) {
$query->where('first_name', 'like', "'%".$agent_name."%'");
})->select('id','agent_id','file_number','title','first_name','last_name','ref_number','pax_adult','pax_child')->with(array('agent'=>function($query){
$query->select('id','first_name','last_name');
}))->get();
Update your query to:
$query->where('first_name', 'like', "%".$agent_name."%");
You had a extra ' at the start and end of the LIKE.

Eloquent ORM - where(), how to include both parameters as table fields

$data = Booking::join('user', 'booking.user_id', '=', 'user.id')
->join('tickets', 'trail_booking.tkt_id', '=', 'tickets.id')
->where('trail_booking.user_id','=','tickets.user_id')
->paginate();
I am unable to get any result using the above query as the 'where' condition evaluates 'tbl_tickets.user_id'to the value instead of table field.
Should I use whereRaw? Or is there any method to correct this?
Because the third parameter is expected to be a value, it will be escaped and quoted, so you should use whereRaw instead:
$data = Booking::join('user', 'booking.user_id', '=', 'user.id')
->join('tickets', 'trail_booking.tkt_id', '=', 'tickets.id')
->whereRaw('trail_booking.user_id = tickets.user_id')
->paginate();
You could alternatively pass the value with DB::raw so it's not escaped:
->where('trail_booking.user_id', DB::raw('tickets.user_id'))
But it's cleaner and more readable with whereRaw in my opinion and it does the same thing.

Resources