some time some variables are null so how check the variable is not null and apply direct in query
$fiter_products = DB::table('products')->DISTINCT('modalid')->DISTINCT('brandid')->select('rimdiameter','modalid','modalname1','modalname2','image1','brand','minprice','maxprice')->where('hubbore','>=',$centre_bore)->where('boltpattern',$boltptn)->where('rimdiameter', $diameter)->where('rimwidth', $width)->where('rimwidthfront', $frontwid)->where('construction', $construct)->where('modalname2', $color)->where('brand', $brand)->get();
any way to solve this issue ?
You may pass another Closure as the third parameter to the when method. This Closure will execute if the first parameter evaluates as false.
$fiter_products = DB::table('products')
->DISTINCT('modalid')
->DISTINCT('brandid')
->select('rimdiameter','modalid','modalname1','modalname2','image1','brand','minprice','maxprice')
->when($centre_bore, function($query, $centre_bore) {
$query->where('hubbore','>=',$centre_bore);
})->when($boltptn, function($query, $boltptn) {
$query->where('boltpattern',$boltptn);
})...
You can use the whereNotNull('field) method. From the docs:
The whereNotNull method verifies that the column's value is not NULL
$product = DB::table('products')->DISTINCT('modalid')->DISTINCT('brandid')->select('rimdiameter','modalid','modalname1','modalname2','image1','brand','minprice','maxprice');
if(isset($centre_bore)){
$product->where('hubbore','>=',$centre_bore);
}
if(isset($boltptn)){
$product->where('boltpattern',$boltptn);
}
if(isset($diameter)){
$product->where('rimdiameter', $diameter);
}
if(isset($width)){
$product->where('rimwidth', $width);
}
if(isset($frontwid)){
$product->where('rimwidthfront', $frontwid);
}
if(isset($construct)){
$product->where('construction', $construct);
}
if(isset($color)){
$product->where('modalname2', $color);
}
if(isset($brand)){
$product->where('brand', $brand);
}
$fiter_products = $product->get();
Related
Is this:
$paginate = $request->get('paginate');
Equivalent to this, for getting a query param if it is present or assign to the associated variable "null" it it is not present:
if ($request->has('paginate')) {
$paginate = $request->get('paginate');
} else {
$paginate=null;
}
According to get() method documentation:
This method belongs to Symfony HttpFoundation and is not usually needed when using Laravel.
Alternatively you can use filled and $request->paginate
So it checks if the request has the "item"and it has value.
$paginate = null;
if ($request->filled('paginate')){
$paginate = $request->paginate;
}
Need help here i m trying to take values from checkbox s and put them into a edit to change the value "Estado" to "Expedido" but when I run the code it gives me the error"Call to a member function update() on null".I also tried find and change the default of find() to the column i want and also tried with raw postgresql code.
$chassis= $request->chassis;
$escala = $request->escala;
if(Auth::check() == true)
{
foreach($chassis as $chassis)
{
$edit = expedicoes::whereIn('Chassis', explode(',',$chassis))->first()->update(['Estado' =>'Expedido']);
}
The method update(Array) doesnt exist on a model instance, it only exists on a query builder as a Builder instance.
either remove the first() call to call update on the query builder
$edit = expedicoes::whereIn('Chassis', explode(',',$chassis))->update(['Estado' =>'Expedido']);
Or update the on the model then call save()
$edit = expedicoes::whereIn('Chassis', explode(',',$chassis))->first();
if ($edit) {
$edit->Estado = 'Expedido';
$edit->save();
}
I suggest you remove the update call from the foreach loop, gather all "chassis" and run a single query
$extractedChassis = [];
foreach($chassis as $chassi)
{
$extractedChassis = array_merge($extractedChassis , explode(',',$chassi));
}
expedicoes::whereIn('Chassis', $extractedChassis)->update(['Estado' =>'Expedido']);
this code return null
expedicoes::whereIn('Chassis', explode(',',$chassis))->first()
so you have no items that Chassis in explode(',',$chassis)
to solve this you can check if its not return null then update it
$expedicoes = expedicoes::whereIn('Chassis', explode(',',$chassis))->first();
if(expedicoes ){
expedicoes ->update(['Estado' =>'Expedido']);
}
but i think your problem in column name so try to use chassis instead of Chassis
and estado instead of Estado
I have this function in my controller that checks parameter requests and saves it into my table for tracking. However my if condition is quite too long because whenever a new request will be added I have to write individual if condition to each request.
Here is my code:
public function storeTracking(Request $request)
{
$traffic = new TrafficTracking();
if ($request->has('gclid')) { // check if request = gclid
$traffic->traffic_type = 'gclid';
$traffic->traffic_value = $request->gclid;
}
if ($request->has('token')) { // check if request = token
$traffic->traffic_type = 'token';
$traffic->traffic_value = $request->token;
}
if ($request->has('fbclid')) { // check if request = fbclid
$traffic->traffic_type = 'fbclid';
$traffic->traffic_value = $request->fbclid;
}
if ($request->has('cjevent')) { // check if request = cjevent
$traffic->traffic_type = 'cjevent';
$traffic->traffic_value = $request->cjevent;
}
$traffic->save();
return response()->json([
'message' => 'success'
], 200);
}
Is there any shorter approach for this one for the if condition? Because the code will be long whenever a new request is added in my storeTracking function in the controller.
you can try like this but you need to way to validate or in try catch you need to handle
this code can be like this
foreach ($request->except('_token') as $key => $value) {
$traffic = new TrafficTracking();
$traffic->traffic_type = $key;
$traffic->traffic_value = $value;
$traffic->save();
break; // if you want single time execution
}
NOTE i m not sure it is correct answer but it is an idea to solve this
Using Ternary Operators
(Condition) ? (Statement1) : (Statement2);
Condition: It is the expression to be evaluated which returns a boolean value.
Statement 1: it is the statement to be executed if the condition
results in a true state.
Statement 2: It is the statement to be executed if the condition
results in a false state.
Using switch case
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
Converting SQL to Laravel format is still a bit confusing for me. I need to use OR. If onloan = NULL OR 0 then display record.
The code below can only detect the 0 but not the NULL.
public function finditembarcode () {
if ($finditembarcode = \Request::get('q')) {
$itembarcode = Item::where(Item::raw("BINARY `itembarcode`"), $finditembarcode)
->where('onloan','=', NULL OR 0)
->paginate();
}else{
$itembarcode = ''; //If nothing found, don't return anything.
}
return $itembarcode;
}
I also tried but get the same result as code above.
->where('onloan','=', NULL || 0)
Laravel Database Query Builder whereNull / whereNotNull / orWhereNull / orWhereNotNull
->where('onloan', 0)->orWhereNull('onloan')
i want to passing data to view
my controller
public function create()
{
$maxcode=Product::selectRaw('MAX(RIGHT(product_code, 3)) as max')->first();
$prx='P2018-';
if($maxcode->count()>0)
{
$tmp = ((int)$maxcode->max)+1;
$newcode = $prx.sprintf("%03s", $tmp);
}
else
{
$newcode = $prx."P2018-001";
}
return $newcode;
return view('product.create', $newcode);
}
my view
...other code...
#foreach($newcode as $nc)
{{$nc->newcode}}
#endforeach
---other code...
the code works well, but the only results appear, the other code on the view does not work.
someone might be able to help me
remove return $newcode;
return view('product.create')->with('newcode',$newcode);
$newcode variable is not an array, you can not use foreach.
use {{$newcode}} in blade to display value of variable.
remove return $newcode; line and change your return function like this,
return view('product.create', compact('newcode'));