Condition in a search query doesn't work - laravel

I have a search query fonction who seems to not work with the last condition . (->where('structure_id' , '=' , $mastructure);
I fact when i run the query i get also the other licences from other "structure_id "
here my search controller
public function getLicenciesStructure(Request $request){
$mastructure = Auth::user()->structure->id ;
$search = $request->get('recherche');
if ($search) {
$query = Licencies::Where('lb_nom', 'like', "%$search%")->orWhere('num_licence' , 'like' , "%$search%")->where('structure_id' , '=' , $mastructure);
}
$licenciestructures = $query->paginate(10)
->appends(['recherche' => $search]);
return view('licencie_structure/index' , compact('licenciestructures' , 'mastructure'));
}
Someone have an idea why the query display also the other items from other structure_id ? thanks a lot in advance

Use where() closure:
Licencies::where(function($q) use($search) {
$q->where('lb_nom', 'like', '%'.$search'%')
->orWhere('num_licence', 'like', '%'.$search.'%');
})->where('structure_id', $mastructure);

Related

Sort 'search results' based on the position of the 'search term' in them

I want to order the results showing those that start with the letters the user is typing first.
public $search = "";
public function render()
{
$business = Empresa::where("name", "like", "%" . $this->search . "%")
->orderBy("name")
->take(5)
->get();
return view('livewire.general.simpleSearch', compact("business"));
}
My website currently shows the results like this:
Instead, when the user types mer I want to display the results like this:
public function render()
{
$search = $this->search;
$business = Empresa::where("name", "like", "%$search%")
->get();
$sorted = $business->sortBy(function ($result) use ($search) {
return strpos($result->name, $search);
})->take(5);
return view('livewire.general.simpleSearch')
->with('business', $sorted->values()->all());
}
You try this
Empresa::where("name", "like", $this->search . "%")->take(5)->get();
You should not put % before your query
$business = Empresa::where("name", "LIKE", $this->search . "%")->orderBy("name")->take(5)->get();
In sql when using LIKE query % act as wildcard. If you put % before your search string it will search ending with your string, if you put % after your search string it will search starting with your string. Since you put % before and after your search string it will search every row contains your search query.

Laravel combine query with if statement

Im working on product filtering using AJAX. Is there any possible way to produce same output as picture shown below using query builder?
I have tried union but it’s not working.
I hope this example gives idea , Try this one
use multiple if statement and get data into DB using joins .
function datatables($request) {
$data = $this->leftJoin('blog_category', 'blog_category.blog_category_uuid', '=', 'blog_detail.blog_category_uuid')
->where('blog_detail.blog_detail_is_deleted', 'NO');
if ($request->search['value'] != null && $request->search['value'] != '') {
$keyword = $request->search['value'];
$data = $data->where(function($query) use ($keyword) {
// $query->orWhere('activity_type.activity_type_name', 'LIKE', '%' . $keyword . '%');
$query->orWhere('blog_detail.blog_detail_title', 'LIKE', '%' . $keyword . '%');
});
}
if (isset($request->order[0]['dir'])) {
$data = $data->orderBy('blog_detail.blog_detail_id', $request->order[0]['dir']);
} else {
$data = $data->orderBy('blog_detail.blog_detail_created_date');
}
$datacount = $data->count();
$dataArray = $data->select('blog_detail.*', 'blog_category.blog_category_name' , DB::raw('DATE_FORMAT(blog_detail.blog_detail_created_date,"%Y-%m-%d") as blog_detail_date'));
if ($request->length == -1) {
$dataArray = $dataArray->get();
} else {
$dataArray = $dataArray->skip($request->start)->take($request->length)->get();
}
return [$datacount, $dataArray];
}
In laravel you can create a model for product say Product. Then the query will be like
$products = Product::where('product_status', '1');
if ($request->input('minimum_price') && $request->input('maximum_prize')) {
$products = $products->whereBetween('product_prize', array($request->input('minimum_price'), $request->input('maximum_prize')));
}
if ($request->input('brand')){
$brand_filter = implode("','", $request->input('brand'));
$products = $products->whereIn('product_brand', $brand_filter);
}
$products = $products->get();
after the execution $products contains the products after query.

How to do a where() on any column in model

I have four columns that I want to do a LIKE query on, user_id,client_id, and start_time, and end_time
I have the current statement
Model::where('client_id','LIKE','%'.$q.'%')
However, instead of it being client_id, I'd like to be able to search through all four of those columns rather then just one, without using a whereOr as that does not work efficiently for my problem
Thank you in advance.
You may do like this below.
Model::where(function($q) {
$q->where('user_id','LIKE','%'.$q.'%')
->orWhere('client_id','LIKE','%'.$q.'%')
->orWhere('start_time','LIKE','%'.$q.'%')
->orWhere('end_time','LIKE','%'.$q.'%');
});
Hope that helps.
You can use when and filter if you have setted one or another variable:
Model::when(isset($client_id),function($query) use ($client_id) {
$query->where('client_id','=', $client_id)
})
->when(isset($user_id),function($query) use ($user_id) {
$query->where('user_id','=', $user_id)
})
->when(isset($start_time),function($query) use ($start_time) {
$query->where('start_time','=',$start_time)
})
->get();
You can use where and Search records
$user_id = $request->user_id ? '%' . $request->user_id . '%' : '';
$client_id = $request->client_id ? '%' . $request->client_id . '%' : '';
$modal = Modal::where(function($q) use($user_id, $client_id) {
$q->where("user_id", "like", "$user_id")
->orwhere("client_id", "like", "$client_id");})->get();
dump($modal->count()); // count display
Hope that helps.

Laravel search with localization

I have a scope about searcing. Also using localization from mcamara
Right now, when i am search something, results are coming with different languages. But i can just search with my main language. After results are came translation happening. This is related with my blade.
{!! $news->translate($localeCode)->title !!}
But main question is before the search. How can i search something with selected language. I can switch language easily. This is my search scope.
public function scopeSearch($query, $search) {
$localeCode = LaravelLocalization::getCurrentLocale();
return $query->translate($localeCode)->where('title', 'LIKE', '%' .$search. '%')
->orWhere('sub_body', 'like', '%' .$search. '%')
->orWhere('body', 'like', '%' .$search. '%');
}
Also this is my controller
public function index (Request $request){
$localeCode = LaravelLocalization::getCurrentLocale();
$updates = Update::latest()->take(3)->get();
$query = $request->get('q');
$new = $request->filled('q') ? News::search($query) : News::query();
$new = $new->orderBy('id', 'desc')->paginate(10);
return view('frontend.news', compact('localeCode', 'updates', 'new', 'query'));
}
I'll be glad if anyone help. Thanks advance.

Parameters Group on eloquent query

I have little problem with my search query at the moment. When i run a search query with only the parameter "comite_id" it's working but when i want to run the search query with "comite_id" and the whereBetween with "date_min" and "date_max" the result is not right , it's seems the result don't care about the whereBetween clause because i don't get the right date range.
Someone knows where i'm doing wrong? thanks a lot in advance
Here my controller :
public function getRetrocession(Request $request){
$comites = Structure::where('type_structure_id' , '=' ,'3')->pluck('nom_structure' , 'id');
$search = $request->input('comite_id');
$dt_min = $request->input('dt_min');
$dt_max = $request->input('dt_max');
if ($search) {
$query = Retrocession::where('comite_id', '=', $search)
->orWhere(function ($q) use($search , $dt_min , $dt_max) {
$q->where('comite_id', '=', $search)
->whereBetween('dt_retrocession', [ $dt_min , $dt_max]);
});
}else {
$query = Retrocession::select();
}
$retrocessions = $query->orderBy('dt_retrocession', 'DESC')->paginate(10)
->appends(['recherche' => $search]);
return view('retrocession/index' , compact('retrocessions' , 'comites'));
}
Because you are using orWhere() it always call both where() and orWhere(); thus including the result of where() and orWhere(). You can try this way
$query = Retrocession::where('comite_id', $search)
->when($dt_min && $dt_max, function($q) use ($dt_min, $dt_max) {
$q->whereBetween('dt_retrocession', [$dt_min, $dt_max]);
});
So you always run the first where, and only include whereBetween() WHEN $dt_min and $dt_max are both true.
Bonne chance Mathieu.

Resources