I'm implementing a search bar that can search customers by first name, last name, or both. So, for example, Mike Hizer would be matched by Mike, Hizer, zer, Mike Hizer, etc. Here's what I came up with:
Customer::where(
DB::raw('concat(first_name," ",last_name)'), 'like', "%{$request->search}%"
)->get()
It works. But is there a more Eloquent approach to combine these two columns (first_name and last_name) without resorting to the DB facade? Would something like
->where(['first_name','last_name'], 'like', "%{$request->search}%")
be possible to achieve?
If you don't want to use the DB facade you could use the whereRaw method:
Customer::whereRaw('concat(first_name," ",last_name) like ?', "%{$request->search}%")->get();
Hope this helps!
$query = User::where('id', '=', '2');
$query->where('first_name', 'LIKE', "%$search%");
$query->or_where('last_name', 'LIKE', "%$search%");
$users = $query->get();
Try this way..
Related
I have 3 tables that are connected/have relation.
Posts table have many tag and one Category
Category table have many Posts
Tag table have many Posts
i want a search feature, i know how to search only use Posts (based on title).
I tried to search each tables with Where in my controller but still no luck.
public function Search(Request $request)
{
$search = $request->search;
$posts = post::where('title', 'like', "%{$search}%")->paginate(5);
return view('search', compact('posts'))->with('result', $search);
}
For example
i have a post Titled 'Test' and with Category 'Tost' and with Tags 'Tast and Tust'
so if i type either the title, category or tags i want it to show up. how can i achieve it?
Try use whereHas method (for further info check https://laravel.com/docs/5.8/eloquent-relationships#querying-relations)
So would become something like:
Post::query()
->where('title', 'like', "%$search%")
->orWhereHas('categories', function ($query) use ($search) {
$query->where('name', 'like', "%$search%");
})
You must use like this.
Post::where('title', 'like', '%' . Input::get('search') . '%')->get();
I have 2 tables: auctions and properties. a one-to-many relation. each auction contains many properties.
I'm trying to query properties directly but I need to apply WHERE clause to 2 columns: property.desc and auction.code using LIKE.
This is what I have:
Property::with(['auction'=>function($query) use ($search) {
$query->orWhere('code', 'LIKE', "%$search%");
}])->
where('desc','LIKE', "%$search%")
->paginate(10);
The problem is that the first query eloquent executes is:
SELECT count(*) as aggregate FROM "properties" WHERE "desc"::text LIKE '%df3432sf%'
Since that query doesn't fetch anything, Eloquent doesn't go any further (I expect applying the filter over auction.code as well.
This is easily done with raw SQL:
select * from properties p
JOIN auctions a ON a."id" = p.auction_id
WHERE a.code LIKE '%x%'
OR p.desc LIKE '%x%'
Any idea?
Thanks.
May be this will work as I am not sure about your flow of data of your application
code:
Property::with('auction', function($q) use($search) {
$q->where('code', 'LIKE', "%".$search."%");
})->where('desc','LIKE', "%".$search."%")->get();
But this will gives only those auction ( optional ) whose property desc will match your search parameter which will make the whole query little complex so I will recommend to go through auction to properties because auction has many properties as you mentioned.
Assuming your auction model is name Auction
Code:
Auction::with('properties', function($q) use ($search) {
$q->where('desc','LIKE', "%".$search."%");
})->where('code', 'LIKE', "%".$search."%")->get();
or If you need even more power, you may use the whereHas and orWhereHas methods to put "where" conditions on your has queries
Property::where('desc','LIKE', "%".$search."%")->orWhereHas('auction', function($q) use($search){ $q->where('code', 'LIKE', "%".$search."%") })->get();
Hope this helps.
I would do something like:
Property::whereRaw('LOWER(`desc`) LIKE ?', '%'.strtolower($search).'%')
orWhereHas('auction', function($query) use ($search) {
$query->whereRaw('LOWER(`code`) LIKE ?', '%'.strtolower($search).'%')
})->paginate(10);
I did a search for a Model (Startup), next step is to order results of the search by DESC or ASC (it doesn't matter now)
My current code is:
$startups = Startup::whereHas('category', function ($query) use ($search, $sort_type) {
$query
->where('name', 'like', "%$search%")
->orderBy('name', $sort_type);
})
->orWhere('description', 'LIKE', "%$search%")
->orWhere('url', 'LIKE', "%$search%")
->get();
return StartupResource::collection($startups);
Explanation of the code:
as you see at the beginning I'm using "whereHas" to search also for coincidences in related model - "Category".
then inside of "whereHas" I'm trying to apply 'orderBy('name', $sort_type)' but it doesn't work properly (it doesn't sort by categories)
I know that we can create method in Startup model and sort it inside of the method, but the problem is that I have to pass variables to the method ( $sort_type, $search) and I don't know how to do this
So how to sort Startups model including related Category model by ASC or DESC in my case ?
Thank you guys a lot for any ideas and help!
That's not possible with whereHas(). You can use a JOIN:
$startups = Startup::select('startups.*')
->join('category', 'category.id', '=', 'startups.category_id')
->where('category.name', 'LIKE', "%$search%")
->orWhere('startups.description', 'LIKE', "%$search%")
->orWhere('startups.url', 'LIKE', "%$search%")
->orderBy('category.name', $sort_type)
->get();
I have this search query for Post model in Laravel:
return $q ->where('title', 'like', '%'.$keyword.'%')
Which works totally fine. In my Post model I also have a relation database categories. This is available as an object in my returned post object.
Is there a way I can query the category -> name?
I have tried:
return $q ->where('category->name', 'like', '%'.$keyword.'%')
I believe what you are looking for can be found here: https://laravel.com/docs/5.6/eloquent-relationships#querying-relations
For your use case you need to make sure you have the relationship established in your Post model. If that is set your query should look similar to the following:
return $q->where('title', 'like', '%'.$keyword.'%')->whereHas('category',
function ($query) use ($keyword) {
$query->where('name', 'like', '%'.$keyword.'%');
})->get();
Hope this helps!
If you're db is well structured - then will want to use Laravel's ORM for this stuff.
https://laravel.com/docs/5.6/eloquent#introduction
I assume that you have Models:
Post
Category
Also that you have category_id as a foreign key in Post model.
This is basic structure.
I assume that you want to search Posts and Categories same time so you can do something like this:
return Post::leftJoin('categories', 'posts.category_id', '=', 'categories.id)
->where('categories.name', 'like', '%' . $keyword . '%')
->orWhere('posts.title', 'like', '%' . $keyword . '%')
->get();
I have a query:
Posts::whereHas('comments', function($query){
$query->where('name', 'like', 'asd');
})->with('comments')->get();
It returns all posts that have comments which have name like 'asd' with all comments of these posts. Can I use the above constraint in '::whereHas' for the 'with' method, so it would not return all comments, but only those that match the requirements? Or do I have to duplicate the query?
You can create query something like this, almost without duplicate :)
$callback = function($query) {
$query->where('name', 'like', 'asd');
}
Posts::whereHas('comments', $callback)->with(['comments' => $callback])->get();
Or, you can make like in this post Laravel - is there a way to combine whereHas and with