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);
Related
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();
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();
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');
Given this code:
$objResellerList - DB::table('ResellerMaster as RM')
->leftJoin('StateMaster as SM','SM.id',RM.id_StateMaster')
->leftJoin('RegionMaster as REM','REM.id','=','RM.id_RegionMaster')
->leftJoin('DealerGroupMaster as DGM','DGM.id','=','RM.id_DealerGroupMaster')
->when($keywords, function($query) use ($keywords) {
return $query->where('RM.company_name', 'like', '%'.$keywords.'%')
->orWhere('DGM.name', 'like', '%'.$keywords.'%')
->orWhere('RM.email', 'like', '%'.$keywords.'%')
->orWhere('RM.pic', 'like', '%'.$keywords.'%')
->orWhere('RM.pic_mobile_no', 'like', '%'.$keywords.'%');
})
->when($status, function($query) use ($status) {
return $query->where('RM.status', $status);
})
->select('RM.*','SM.name as state','REM.name as region', 'DGM.name as dealer_group')
->orderby('RM.company_name','asc')
->paginate(10);
When i search with keywords, I need to close it with a bracket at when condition. How to include the bracket at when condition?
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
give u brackets
select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
so use where with function
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
or
->where(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
it will give u brackets
use
->when($status,function($query) {
return $query->where(function ($query) {
//do some action
})
})
I want do following query in laravel elequent
select from table where cond1 and(cond or cond or cond..)and cond3.
I have used following code,
$invoice = Invoice::with("items", "salesPerson", "client");
if ($q = Request::input("q")) {
$invoice->where("invoices.invoice_number", 'LIKE', "%$q%");
$invoice->orWhere("invoices.reference_number", 'LIKE', "%$q%");
$invoice->orWhere("invoices.client_name", 'LIKE', "%$q%");
$invoice->orWhere("invoices.invoice_date", 'LIKE', "$q%");
$invoice->orWhere("invoices.due_date", 'LIKE', "$q%");
}
$invoice->whereNull("invoices.deleted_at");
but it return deleted records as well.How can I fix this?
In this case a nested where comes in handy:
$invoice = Invoice::with("items", "salesPerson", "client");
if ($q = Request::input("q")) {
$invoice->where(function($query) use ($q){
$query->where("invoices.invoice_number", 'LIKE', "%$q%");
$query->orWhere("invoices.reference_number", 'LIKE', "%$q%");
$query->orWhere("invoices.client_name", 'LIKE', "%$q%");
$query->orWhere("invoices.invoice_date", 'LIKE', "$q%");
$query->orWhere("invoices.due_date", 'LIKE', "$q%");
});
}
$invoice->whereNull("invoices.deleted_at");