Laravel: How to use multiple where with Request - laravel

This code is working. It checks for the exact barcode, case sensitive:
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
->where('debarred', NULL)
->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}
However, I need to add one more WHERE clause to check if Expiration is greater than today. I tried this but it does not work.
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
->where('debarred', NULL)
->where('expiration','>',new Date())
->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}

This should work:
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
->where('debarred', NULL)
->whereDate('expiration','>', date('Y-m-d'))
->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}
PS: as far as I understand, your question isn't about multiple "where" conditions in the request, as you've already achieved that in the working example, instead, it's about making a filter for date values.

Check Laravel 7.x Where Clauses docs(still true for version 5.x and 6.x)
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$whereClauses= [[Patron::raw("BINARY `barcode`"), '=', $findpatronbarcode],
['debarred','=', NULL],
['expiration','>', Carbon::now()]];
$patronbarcode = Patron::where($whereClauses)->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}

Related

Where function query withcount not working Laravel

this works perfectly fine
return Webinar::query()
->withCount('users')->orderByDesc('users_count')
->get();
but I need to use a condition for sorting, but this doesnt work
return Webinar::query()
->where(function($query) use ($sort_by) {
if($sort_by == 0) {
return $query->withCount('users')->orderByDesc('users_count');
} else {
return $query->withCount('review')->orderByDesc('review_count');
}
})
->get();
I also tried a condition inside like this to check my if else function
return Webinar::query()
->where(function($query) use ($sort_by) {
if($sort_by == 0) {
return $query->withCount('users')->orderByDesc('users_count')
->where('status', 2);
} else {
return $query->withCount('review')->orderByDesc('review_count')
->where('status', 1);
}
})
->get();
but the where status is only working not withcount. I hope someone can answer, thank you so much
How about you move condition code outside query
but I need to use a condition for sorting, but this doesnt work
if ($sort_by === 0) {
$order = 'users_count';
$count = 'users';
} else {
$order = 'review_count';
$count = 'review';
}
return Webinar::query()
->withCount('users')->orderByDesc($order)
->get();

If clauses between where composition to laravel collect

Situation:
$post_1 = 'john';
$post_2 = 30;
$arr = array(['name'=>'john','number'=>70],['name'=>'clark','number'=>50]);
$collection = collect($arr);
if($post_1 == 'john')
{
$collection->where('name',$post_1);
}
if($post_2 == 70)
{
$collection->where('number',$post_2);
}
var_dump($collection->all());
But this doesn't work. I want to include filters but it depends on the post parameters to exist.
I think you can use when
$result= $collection->when(($post_1=="john"), function ($collection)use($post_1) {
return $collection->where('name',$post_1);
})->when(($post_2 == 70), function ($collection)use($post_2) {
return $collection->where('number',$post_2);
})->all();
dd($result);
or
$collection->when(($post_1=="john"), function ($collection)use($post_1) {
return $collection->where('name',$post_1);
})->when(($post_2 == 70), function ($collection)use($post_2) {
return $collection->where('number',$post_2);
});
dd($collection->all());
Ref:https://laravel.com/docs/8.x/collections#method-when

Best way convert json string to Laravel Query Builder?

i have a filter string, what do you think how can i convert laravel query builder? I used some ways but I wasn't sure.
filter: [["id","=",2],"or",["id","=",3],"and",["name","LIKE", "%John%"]]
Thank you.
if ($request->get('filter')) {
$filter = json_decode($request->get('filter'));
if (gettype($filter[0]) === 'string') {
list($field, $id) = $filter;
$query->where($field, $id);
} elseif (gettype($filter[0]) === 'array') {
foreach ($filter as $value) {
if (gettype($value) === 'string') {
switch ($value) {
case 'or':
//
break;
case 'and':
//
break;
}
} elseif (gettype($value) === 'array') {
list($field, $clause, $id) = $value;
}
}
}
}
Maybe something like this:
$arr = [["id","=",2],"or",["id","=",3],"and",["name","LIKE", "%John%"]];
$query = Model::query(); // build the empty query
if(size($arr) > 0){ // if there is at least 1 element
$query->where(...$arr[0]); // apply that element
for($i = 1; $i < size($arr); $i+=2){ // for every other entry, loop through them 2 by 2 and based of the current one, apply orWhere or where
if($arr[$i] === "or")
$query->orWhere(...$arr[$i+1]);
else
$query->where(...$arr[$i+1]);
}
}
$result = $query->get();

Laravel each() method

I am trying to understand why the following is always returning false:
public function checkDate($transactions)
{
$start = Carbon::now()->subMonth();
$end = Carbon::now();
$transactions->each(function ($transaction) use ($start, $end) {
$date = Carbon::createFromFormat('Y-m-d', $transaction->date);
if ($date->between($start, $end))
{
return true;
}
return false;
});
return false;
}
When I remove both the return false, then null is being displayed.
When I use a regular foreach, it works:
$start = Carbon::now()->subMonth();
$end = Carbon::now();
foreach($transactions as $transaction)
{
$date = Carbon::createFromFormat('Y-m-d', $transaction->date);
if($date->between($start, $end))
{
return true;
}
return false;
}
The thing what I don't understand is, when I do the following:
public function checkDate($transactions)
{
$start = Carbon::now()->subMonth();
$end = Carbon::now();
$transactions->each(function ($transaction) use ($start, $end) {
$date = Carbon::createFromFormat('Y-m-d', $transaction->date);
if ($date->between($start, $end))
{
dd('test');
return true;
}
return false;
});
return false;
}
Then "test" is being displayed.
Can someone explain as to why this is happening, since I don't understand it.
The each method doesn't stop iterating when true is returned. It does stop, however, when false is returned.
If you would like to stop iterating through the items, you may return false from your callback:
Collection Methods - each()
Your method only returns false also.
If you are trying to verify all transactions fall with a date range, then use the every method:
// returns true if all transactions date are between start and end, otherwise returns false.
return $transactions->every(function ($transaction) use ($start, $end) {
$date = Carbon::createFromFormat('Y-m-d', $transaction->date);
return $date->between($start, $end);
});

How to display last query in laravel 5?

I use :
DB::connection()->enableQueryLog();
var_dump(DB::getQueryLog());
But it's not working.
My service is like this :
public function dataCustomerList($param)
{
$status = $param['status_sort'];
$gender = $param['gender'];
$published = $param['published'];
if($published=='NO'){
$published_check = 'NO';
$published = 0;
}
else if($published=='YES'){
$published_check = 'YES';
$published = 1;
}
DB::connection()->enableQueryLog();
$customer = customer::paginate(10);
if(!empty($status)) {
// die($status);
$customer = customer::where('tb_customer.customer_status', '=', $status)->paginate(10);
}
if (!empty($gender)) {
$customer = customer::where('tb_customer.gender', '=', $gender)->paginate(10);
}
if (!empty($published_check)) {
$customer = customer::where('tb_customer.published', '=', $published)->paginate(10);
}
var_dump(DB::getQueryLog());
return $customer;
}
When I run it, query does not appear
The result is like this :
array(0) {
}
How to get the query executed in Laravel 5?
Thank you very much
You could listen to query events on DB:
DB::listen(function($query) {
var_dump($query->sql);
});

Resources