Removing existing Where clauses - laravel

I want to remove where clauses in some conditions:
$q = Thread::with('comments')->take(10);
if(strlen($input) < 4){
$result = $q->where('title', '~', "^$input$")->get();
if($result->isNotEmpty()){
return $result;
}
// if was empty:
}
// How to clean the where clause from the above here? because it affect the query below:
$result = $q->where('title', 'like', "%$input%")->get();
The problem is the first where clause affects the second one if the data was empty, How can i remove existing where clauses when needed ? also the newQuery() is not working in my case.
Note that i'm using two seperate statement in postgres ~ and 'like'
Something like reorder() for where clauses

Yes there is a way to do it
$q = Thread::with('comments')->take(10);
if(strlen($input) < 4){
$result = $q->where('title', '~', "^$input$")->get();
if($result->isNotEmpty()){
return $results;
}
}
// getQuery() is a query builder method that contains all the groupings, selects, orders, wheres, joins etc for the query that you are accessing or trying to build.
$q->getQuery()->wheres= [];
$result = $q->where('title', 'like', "%$input%")->get();

Use Conditional Clauses
$threads = Thread::with('comments')->when(strlen($input) < 4, function ($query) use ($input) {
return $query->where('title', '~', "^$input$");
}, function ($query) use ($input) {
return $query->where('title', 'like', "%$input%");
})->take(10)->get();
https://laravel.com/docs/8.x/queries#conditional-clauses
If you want to give it a second change I would write that logic like this. Comments are loaded only if they are needed.
if (strlen($input) < 4) {
$threads = Thread::where('title', '~', "^$input$")->take(10)->get();
if ($threads->isNotEmpty()) {
return $threads->load('comments');
}
}
return Thread::with('comments')->where('title', 'like', "%$input%")->take(10)->get();

if you cloning before add where
it works the same as deleting where
...
if(strlen($input) < 4){
$result = (clone $q)->where('title', '~', "^$input$")->get();
if($result->isNotEmpty()){
return $results;
}
}
$result = $q->where('title', 'like', "%$input%")->get();
...

Related

Multiple keywords search sql sentence not showing in Laravel8

I'm studying keyword search.
I successed single keyword search and now I'm trying to make multiple one.
as Half-width space keyword split.
I use this Middlewarefrom this website
https://qiita.com/logue/items/64c4b5545f76ef17de70
When I dd() . I can see keyword split are good.
but when I search function runs There is no sql sentence appear
and I can display empty result page.
Could you teach me correct code please?
Controller
public function order_adv()
{
$keyword = request('search_adv');
if(!empty($keywords)) {
foreach ($keywords as $keyword) {
$products = Product::when('w_m','like','%'.$keyword.'%');
}
}
return view('products.result_adv' , compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
WEB.php
Route::get('products/search_adv', [ProductController::class, 'search_adv'])->name('search_adv');
Route::post('products/search_adv', [ProductController::class, 'order_adv'])->name('products.search_adv')->middleware('keyword');
You can use where call back method
public function order_adv()
{
$keywords =explode(' ',request('search_adv'));
$products = Product::when(count((array)$keywords),function($query)use($keywords){
$query->where(function($query)use($keywords){
foreach ($keywords as $keyword) {
$query->orWhere('w_m','like','%'.$keyword.'%');
}
});
})->get();
return view('products.result_adv' , compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
$keywords = request('search_adv');
if (count($keywords) > 0) {
$products = Product::where(function($query)use($keywords) {
foreach ($keywords as $keyword) {
$query->orWhere('w_m', 'LIKE', "%{$keyword}%");
}
})->get();
}
In your case use where Clauses,
https://laravel.com/docs/8.x/queries#where-clauses
use when in condition statement,
https://laravel.com/docs/8.x/collections#method-when

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.

Eloquent where condition AND ( condition OR condition OR condition )

Can somebody tell me what am I doing wrong?
I want to get all projects with status 0 and if any of data matches in eloquent.
My code is like this
public function search(Request $request){
if($request->has('q')){
$search_query = $request->input('q');
$projects = Project::where(['status' => '0'])->get();
$projects = $projects->where(function($query) use ($search_query) {
$query->where('title','LIKE','%'.$search_query.'%')
->orWhere('shortDescription','LIKE','%'.$search_query.'%')
->orWhere('longDescription','LIKE','%'.$search_query.'%')
->orWhere('tags','LIKE','%'.$search_query.'%')
->orWhere('projectLink','LIKE','%'.$search_query.'%');
});
$projects->get();
dd($projects);
}
}
And I am getting this error
ErrorException
explode() expects parameter 2 to be string, object given
I solved the problem but changed the code a little bit... If you like, take it! I tested and it's working.
if($request->has('q')){
// Your $request->q Value
$term = $request->q;
$projects = DB::table('tbl_projects')->where('status', '=', 0)
->where(function ($query) use ($term) {
$query->where('title', 'LIKE', '%'.$term.'%')
->orWhere('shortDescription', 'LIKE', '%'.$term.'%')
->orWhere('longDescription', 'LIKE', '%'.$term.'%')
->orWhere('tags', 'LIKE', '%'.$term.'%')
->orWhere('projectLink', 'LIKE', '%'.$term.'%');
})
->get();
//dd($projects);
}
Raw query is:
select * from `tbl_projects` where `status` = 0 and (`title` LIKE %bla bla% or `shortDescription` LIKE %bla bla% or `longDescription` LIKE %bla bla% or `tags` LIKE %bla bla% or `projectLink` LIKE %bla bla%))
Regards!
Your error is
$projects = Project::where(['status' => '0'])->get();
(['status' => '0']) Where clause params required is different. You must use ('status', '0') or ('status', '=', '0')
When you use ->get(), you have a Collection or EloquentCollection object. You need a QueryBuilder
Try to implement a better programation logic.
A better implementation is
public function search(Request $request){
if($request->has('q')){
// Add array with field coincidences target. Is better if this array is a const static field in the Project model
$searchFields = ['title', 'shortDescription', 'longDescription', 'tags', 'projectLink' ];
// Now you can add 100 fields for search coincidences easily
$search_query = $request->input('q');
$projects = Project::where('status', '0');
foreach($searchFields as $searchField){
if ($searchField == 'title'){
$projects = $projects->where($searchField,'LIKE',"%$search_query%");
continue;
}
$projects = $projects->orWhere($searchField,'LIKE',"%$search_query%");
}
$projects = $projects->get();
dd($projects);
}
}

How do I use < or > in where clauses on collection in Laravel 5.1

How do I use < or > in where clauses on collection in Laravel? The below is not working:
$company['jobs']->where('created_at','>=',$Start_Date)->where('created_at','<=',$End_Date)
Use the orWhere method:
$company['jobs']->where('created_at', '>=', $Start_Date)
->orWhere('created_at', '<=', $End_Date);
Alternatively, you can use the whereBetween method:
$company['jobs']->whereBetween('created_at', [$Start_Date, $End_Date]);
This is the right solution
$filtered = $company['jobs']->filter(function ($item) use($Start_Date,$End_Date) {
error_log($item['created_at']);
return $item['created_at'] >= $Start_Date && $item['created_at'] <= $End_Date;
});
As #Joseph Silber said, you can create a Scope in your Model to do your own logic. Like this:
// Inside your model
public function scopeSearch($query, $field, $value)
{
if(is_numeric($value))
return $query->where($field, '=', "$value");
elseif(!empty($value))
return $query->where($field, '=', "$value");
else
return $query;
}
Then, you can call it from your controller:
$query = Company::search('year',2016);

Paginate search result laravel

After some help in a previous post Laravel - Search Facility on site using Eloquent I now need to some help on paginating the result using the built in laravel pagination class.
public function search() //no parameter now
{
$q = Input::get('term');
if($q && $q != ''){
$searchTerms = explode(' ', $q);
$query = DB::table('wc_program'); // it's DB::table(), not DB::tables
if(!empty($searchTerms)){
foreach($searchTerms as $term) {
$query->where('JobRef', 'LIKE', '%'. $term .'%');
}
}
$results = $query->get();
dd($results); // for debugging purpose. Use a View here
}
}
Simply change get to paginate and provide number of items per page.
$results = $query->paginate(10);

Resources