I've been stuck onto a problem with laravel, since I'm new to this I try things, and things still don't work.
I have a search function that, seek through the users, using the name, the specialty of the user, or his location, the function work if you fill just one input, if you start to fill a second input, it shows the first condition which is coded in the controller.
Exemple :
if i put
Name : -empty-
Specialty : -empty-
Location : New
It shows me everyone that lives in cities starting with "New", but if I do
Name : Maria
Specialty : -empty-
Location : New
It shows me everyone whose name is Maria, or, I want "Maria" who live in cities starting with "New"
So I search how to make my code work with that, making if inside of if, making double condition, but since I'm with laravel I have trouble doing it (my version is 5.0, i'll go for update after this bug is resolved)
Here is the part where I'm stuck on, with the things I tried,
if($attributs['specialty'] != "") {
$users = User::where('status_user', '=', '1')
->where('specialty_id', $attributs['specialty'])
->where('city', 'LIKE', '%'.$attributs['locality'].'%') //;
/* if($attributs['name']!= ""){
$query->where(function ($query) use ($name){
$query->where(('CONCAT(lastname, " ", firstname)'), 'LIKE', $name.'%')
->orWhere(('CONCAT(firstname, " ", lastname)'), 'LIKE', $name.'%');
})
}
->where('address', 'LIKE', '%'.$attributs['locality'].'%')
->where(DB::raw('CONCAT(address, " ", postal_code, " ", city)'), 'LIKE', '%'.$address.'%')*/
->confirmed()
->get();
}
else if($attributs['name'] != ""){
$users = User::where(function ($query) use ($name) {
$query->where(function ($query) use ($name) {
$query->where(DB::raw('CONCAT(lastname, " ", firstname)'), 'LIKE', $name.'%')
->orWhere(DB::raw('CONCAT(firstname, " ", lastname)'), 'LIKE', $name.'%');
})
->where('status_user', '=', '1')
->confirmed();
})
->orWhere(function ($query) use ($name) {
$query->where(function ($query) use ($name) {
$query->where(DB::raw('CONCAT(lastname, " ", firstname)'), 'LIKE', $name.'%')
->orWhere(DB::raw('CONCAT(firstname, " ", lastname)'), 'LIKE', $name.'%');
})
->where('status_user', '=', '1')
->where('invite', '=', '1');
})
->get();
}
else if($attributs['address'] != ""){
$users = User::where('status_user', '=', '1')
->where('address', 'LIKE', '%'.$attributs['locality'].'%')
->where(DB::raw('CONCAT(address, " ", postal_code, " ", city)'), 'LIKE', '%'.$address.'%')
->confirmed()
->get();
}
Thanks in advance, it's been days since I'm on it ><
Edit : Here's a few more info since it isn't not that clear, sorry about that
I want to make multiple search, the code I present you works, but do not provides multiple search, i tried to make if with multiple condition, and it don't work, i tried to make several where clause with if inside of the first if, and it don't work either, so here I am, thanks a lot in advance, but I'm still stuck ^^'
Try when() conditional operator. https://laravel.com/docs/5.7/queries#conditional-clauses
$users = User::when($attributs['specialty'] != "", function($q){
return $q->where('status_user', '=', '1')
->where('specialty_id', $attributs['specialty'])
->where('city', 'LIKE', '%'.$attributs['locality'].'%')
->confirmed();
})
->when($attributs['name'] != "", function($q){
return $q->where(function ($query) use ($name) {
$query->where(function ($query) use ($name) {
$query->where(DB::raw('CONCAT(lastname, " ", firstname)'), 'LIKE', $name.'%')
->orWhere(DB::raw('CONCAT(firstname, " ", lastname)'), 'LIKE', $name.'%');
})
->where('status_user', '=', '1')
->confirmed();
})
->orWhere(function ($query) use ($name) {
$query->where(function ($query) use ($name) {
$query->where(DB::raw('CONCAT(lastname, " ", firstname)'), 'LIKE', $name.'%')
->orWhere(DB::raw('CONCAT(firstname, " ", lastname)'), 'LIKE', $name.'%');
})
->where('status_user', '=', '1')
->where('invite', '=', '1');
})
})
->when($attributs['address'] != "", function($q){
return $q->where('status_user', '=', '1')
->where('address', 'LIKE', '%'.$attributs['locality'].'%')
->where(DB::raw('CONCAT(address, " ", postal_code, " ", city)'), 'LIKE', '%'.$address.'%')
->confirmed();
})
->get();
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
I'm trying to add a second searchterme that searches for number stars per users here are the table that I have
$searchTerm = $this->searchTerm;
$searchTermes1 = $this->searchTermes1;
$users = User::leftJoin('barbers', 'barbers.id_user', '=', 'users.id')
->whereHas('commandes', function ($query) use ($searchTermes1) {
$query->where('barbers.id', '>', 0)
->selectRaw('count(*) as count')
->havingRaw('count(*) >= ? or adress', [(int)$searchTermes1]);
})
->whereIn('barbers.id_state', [1, 4])
->where('isValidate', true)
->where(function ($query) use ($searchTerm) {
$query->where('barbers.name_barber', 'like', '%'.$searchTerm.'%')
->orWhereRaw("concat(first_name, ' ', last_name) like '%$searchTerm%'")
->orWhereRaw("concat(last_name, ' ', first_name) like '%$searchTerm%'");
})
->orderBy('barbers.created_at', 'desc')->paginate(10);
return view('livewire.barber-pagination', compact(['users']));
I tried this but it is not working
$users = User::leftJoin('avis', 'users.id', '=', 'avis.id_barber')
->leftJoin('barbers', 'barbers.id_user', '=', 'users.id')
->whereHas('commandes', function ($query) use ($searchTermes1,$searchTermes2) {
$query->where('barbers.id', '>', 0)
->havingRaw('count(*) >= ?', [(int)$searchTermes1]);
})
->whereIn('barbers.id_state', [1, 4])
->where('isValidate', true)
->where(function ($query) use ($searchTerm, $searchTermes3) {
$query->where('barbers.name_barber', 'like', '%'.$searchTerm.'%')
->orWhereRaw("concat(first_name, ' ', last_name) like '%$searchTerm%'")
->orWhereRaw("concat(last_name, ' ', first_name) like '%$searchTerm%'")
->havingRaw('sum(avis.rating) >= ?', [(int)$searchTermes3]);
})
->groupBy('users.id')
->orderBy('barbers.created_at', 'desc')
->paginate(10);
return view('livewire.barber-pagination', compact('users'));
I fixed the error but it returns just one result
$searchTerm = $this->searchTerm;
$searchTermes1 = $this->searchTermes1;
$searchTermes2 = $this->searchTermes2;
$users = User::leftJoin('barbers', 'barbers.id_user', '=', 'users.id')
->whereHas('commandes', function ($query) use ($searchTermes1) {
$query->where('barbers.id', '>', 0)
->havingRaw('count(*) >= ?', [(int)$searchTermes1]);
})
->where(function ($query) use ($searchTermes2) {
$query->whereHas('avis', function ($subquery) use ($searchTermes2) {
$subquery->havingRaw('avg(rating) >= ?', [(int)$searchTermes2]);
});
})
->whereIn('barbers.id_state', [1, 4])
->where('isValidate', true)
->where(function ($query) use ($searchTerm) {
$query->where('barbers.name_barber', 'like', '%'.$searchTerm.'%')
->orWhereRaw("concat(first_name, ' ', last_name) like '%$searchTerm%'")
->orWhereRaw("concat(last_name, ' ', first_name) like '%$searchTerm%'");
})
->orderBy('barbers.created_at', 'desc')
->paginate(10);
I am trying to implement a search feature in a site. A user can specify a search string that could match a product_id, product_name or the product_sku. What I want to implement is a search function that would return all that matches.
What I've come with so far:
return Product::with(['getProductItem' => fn ($query) => $query->orWhere('itemSkuNumber', '=', $request->search)])
->where(
'name',
$request->has('match') ? '=' : 'like',
$request->has('match') ? $request->search : '%' . $request->search . '%'
)
->orWhere('id', '=', $request->search)->get();
This has worked if I specify a product_id or product_name but does not return anything if I provide an sku.
You should wrap orWheres with another where because it will try to filter all of them.
Product::with(['getProductItem'])
->whereHas('getProductItem', function ($query) use ($request) {
$query->where(function ($subquery) use ($request) {
$subquery->where('itemSkuNumber', '=', $request->search);
//put your orwhere queries in here
});
})
->get();
Try whereHas
$products = Product::query()
->with(['getProductItem'])
->whereHas('getProductItem', fn ($query) => $query->where('itemSkuNumber', '=', $request->search)]))
->orWhere(
'name',
$request->has('match') ? '=' : 'like',
$request->has('match') ? $request->search : '%' . $request->search . '%'
)
->orWhere('id', '=', $request->search)
->get();
#gguney, #Sumit,
Thanks for your help, :).
Got it working with:
return Product::with(['getProductItem'])
->whereHas('getProductItem', function ($query) use ($request) {
$query->where('itemSkuNumber', '=', $request->search);
})
->orWhere(
'name',
$request->has('match') ? '=' : 'like',
$request->has('match') ? $request->search : '%' . $request->search . '%'
)
->orWhere('id', '=', $request->search)
->get();
#Sumit,
You have extra parentheses and brackets.
I need to search for the code in the appointment table OR the patient name which is the appointment's relation. here is the code I reached so far but it is not working:
$lab = Lab::with(['patient' => function ($q) use ($search_query) {
$q->select('id', 'avatar', DB::raw('CONCAT(first_Name, " ", second_Name) AS name')
->where('name', 'like', "%{$search_query}%")
->orWhereRaw("concat(first_name, ' ', second_name) like '%$search_query%' ")
);
}])
->select('id', 'code')
->Where('code', 'like', "%{$search_query}%")
->limit(5)
->get();
Loding relation is different from applying 'where' on it, you sould load the relation and apply 'whereHas' in another statement:
$lab = Lab::with(['patient' => function ($q) {
$q->select('id', 'avatar', DB::raw('CONCAT(first_Name, " ", second_Name) AS name')
);
}])
->select('id', 'code')
->Where('code', 'like', "%{$search_query}%")
->orWhereHas('patient',function ($query) use ($search_query) {
$query->where('name', 'like', "%{$search_query}%");
})
->limit(5)
->get();
Im trying to figure out why my query is ignoring everything except the title and the description. The search button leading to the controller, is for filtering different type of ads , by category, by region, by price.
For example if now i search for existing ad and its found by title / keyword -> will always show, even if i choose a different region / category/ price range
Im trying to use something that will save me a lot of if statements to check if they exist in the request. Maybe other option si to use https://github.com/mohammad-fouladgar/eloquent-builder to build my query
public function index(Request $request)
{
$keyword = $request['keyword'];
$category_id = $request['category_id'];
$type_id = $request['type_id'];
$region_id = $request['region_id'];
$min_price = $request['min_price'];
$max_price = $request['max_price'];
$result = Ad::when($keyword, function ($q) use ($keyword) {
return $q->where('title', 'like', '%' . $keyword . '%')->orWhere('description', 'like', '%' . $keyword . '%');
})
->when($category_id, function ($q) use ($category_id) {
return $q->where('category_id', $category_id);
})
->when($region_id, function ($q) use ($region_id) {
return $q->where('region_id', '=', $region_id);
})
->when($type_id, function ($q) use ($type_id) {
return $q->where('adtype_id', '=', $type_id);
})
->when($min_price, function ($q) use ($min_price) {
return $q->where('price', '>=', $min_price);
})
->when($max_price, function ($q) use ($max_price) {
return $q->where('price', '<=', $max_price);
})
->paginate(8);
My get param url looks like that:
search?keyword=&category_id=0®ion_id=0&type_id=0&min_price=&max_price=
The produced query in mysql when i search for existing ad by its name and i look for a different category is:
select * from `ads` where `title` like '%test test%' or `description` like '%test test%' and `category_id` = '2' limit 8 offset 0
The ad is found, but the actual category is 1, not 2, same for all others optimal parameters.
You can edit your query to look for specific relations, using whereHas. This method will allow you to add customized constraints to a relationship constraint, such as checking the content of a comment.And to check max/min price, use where method. So, you can use it like this:
$result = Ad::when($keyword, function ($q) use ($keyword) {
return $q->where('title', 'like', '%' . $keyword . '%')->orWhere('description', 'like', '%' . $keyword . '%');
})
->whereHas('category_relation_name', function ($q) use ($category_id) {
return $q->where('category_id', $category_id);
})
->whereHas('region_relation_name', function ($q) use ($region_id) {
return $q->where('region_id', $region_id);
})
->whereHas('type_relation_name', function ($q) use ($type_id) {
return $q->where('adtype_id', $type_id);
})
->where('price', '>=', $min_price);
->where('price', '<=', $max_price);
->paginate(8);
I use this : https://github.com/rinvex/repository
My query is like this :
$query = $this->store_repository
->join('favorites', function ($join) {
$join->on('stores.id', '=', 'favorites.favoritable_id')
->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
})
->where('stores.status', '=', 1)
->select('stores.id', 'stores.name', 'stores.photo','stores.address');
if($location)
$query->where('stores.address', 'like', "%$location%");
if($q) {
$query->where('stores.name', 'like', "%$q%")
->where('stores.address', 'like', "%$q%", 'or');
}
$result = $query->orderBy('favorites.updated_at', 'desc')->paginate($num);
When executed, there exist error like this :
Missing argument 3 for
Rinvex\Repository\Repositories\BaseRepository::join()
How can I solve it?
I don't think you need the clojure to make what you want.
I guess this should be fine :
$query = $this->store_repository
->join('favorites', '=', 'favorites.favoritable_id')
->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
->where('stores.status', '=', 1)
->select('stores.id', 'stores.name', 'stores.photo','stores.address');