query returning wrong result for like'%' - laravel-5

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.

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();

Why, request return symbol — '?'

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();

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.

Select Join Where and orWhere in Laravel 5

I have two tables- jobseekers and resumes. I try to achieve three search options- by entering 1. "first name", 2. "last name", and 3. "first name + last name". My current code is as below:
$q = \Request::get('keyword');
$data['resume'] = Resume::join('jobseekers', 'jobseekers.user_id', '=', 'resumes.user_id')
->where('jobseekers.first_name','like','%'.$q.'%')
->orWhere('jobseekers.last_name','like','%'.$q.'%')
->orderBy('resumes.updated_at','desc')->paginate(50);
Using my texbox (keywords), When I search only last / first name, it works fine. However, when I type both first + last name in the textbox, it shows no result.
Please share me how to achieve this.
Using CONCAT():
->orWhere(DB::raw("CONCAT(jobseekers.first_name, ' ', jobseekers.last_name)"), 'LIKE','%'.$q.'%');
Use where and or where like this
$results = App\Table::select('*')
->where(function ($query) use ($search_term) {
$query->where('column3', 'like', $search_term.'%')
->orWhere('column4', 'like', $search_term.'%')
})
->orderBy('column1', 'asc')
->get();
Hope this help

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