Laravel: Using OR in multiple Where Claus - laravel

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')

Related

Call to a member function update() on null with array/checkbox/edit

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

MODELS: Cannot update values in DB

I have this function to update the DB values, but one of the statements is not working. I created a relation to the table called ventas, however, it is not updating it´s value to NULL. This is my code:
public function cambiarEstatusFactura( Request $request){
try{
\DB::beginTransaction();
$factura = FacturaVenta::with('venta')->where( 'id' , $request->id )->first();
// dd('Esto : ' , $factura->venta->factura_uuid);
if( $factura->estatus == 'Timbrada' ){
$factura->estatus = 'Cancelada';
$factura->uuid = NULL;
$factura->venta->factura_uuid = NULL;
$factura->save();
}
\DB::commit();
}catch (Exception $e){
\Alert::error($e->getMessage())->flash();
return redirect('admin/finanzas/factura/venta');
}
}
The statement that is not working is:
$factura->venta->factura_uuid = NULL;
Though the other statements work seamlessly, this seems to have issues but I do not get any error messages.
Variable $factura contains FacturaVenta model and the relation venta brings the model from the table ventas, that's why I try to access to it via $factura->venta and the commented dd('Esto : ' , $factura->venta->factura_uuid); in the code brings the value correctly, that means that it's pointing correctly to the correct table but it's not being updated to NULL.
I just want to update the value from whatever it has to NULL.
Could you help me please?

Filter a value in all properties with Laravel

This question is very similar to Laravel filter a value in all columns. Sorry, if it turns out as a duplicate later on, but I have another working code to provide.
What does work is filtering on the client side via JavaScript:
filterfunction : function(entry, filter)
{
if(filter != null)
filter.trim().split(' ').forEach(function(item){
if(!this.eachRecursive(entry, item))
return false;
});
return true;
},
eachRecursive: function(obj, localfilter) {
for(const key in obj) {
if (!obj.hasOwnProperty(key))
continue;
if(typeof obj[key] == "object" && obj[key] !== null){
if(this.eachRecursive(obj[key], localfilter))
return true;
}
else
if((obj[key] + "").toLowerCase().indexOf(localfilter.toLowerCase()) != -1)
return true;
}
return false;
},
The filter function is used as the filter function for the Bootstrap-Vue table component like described in custom-filter-function.
The question is now: how to achieve similar functionality in the Laravel-Backend (for using with Livewire)?
I can imagine, that listing all columns via getColumnListing, mentioned in Laravel filter a value in all columns is possible, but this wouldn't suffice, I still need the relations, like in laravel mysql query with multiple where orwhere and inner join.
Currently, I'm trying out to convert the Eloquent object to JSON and then to parse it, as it includes all the loaded relations eloquent-serialization. But this seems like the last resort and a kind of misuse of serialization.
For now I'm going to use the route over converting to json. However, I found a way to not to parse json by regular expressions. Instead, I convert the jsonified collection back to php objects. With them I'm able to reimplement the functions from above:
private function eachRecursive(stdClass $obj, string $localfilter) {
foreach($obj as $key => $val){
if(is_object($val)){
if($this->eachRecursive($val, $localfilter))
return true;
} elseif(is_array($val)){
foreach($val as $k => $v)
if($this->eachRecursive($v, $localfilter))
return true;
} elseif(stripos(strval($val), $localfilter) !== false){
return true;
}
}
return false;
}
private function filterfunction(Collection $collection, string $filter){
$retVal = [];
foreach (json_decode($collection->toJson()) as $entity){
foreach(explode(' ', trim($filter)) as $localfilter)
if(!$this->eachRecursive($entity, $localfilter))
continue 2;
array_push($retVal, $entity->id);
}
return $retVal;
}

laravel get eloquent return null for specific column if another column is not specific word

I have query something like this:
Order::where('order_id', 2)
->leftJoin('test', 'test.order_id', '=', 'orders.order_id')
->get(['order.status, 'order.path', 'test.name']);
Now, what I want to achieve is the following. in get clause, when fetching specific columns, if 'order.status' isn't COMPLETED, return 'order.path' as null, if 'order.status' is COMPLETED, then return actual order.path
You should try to loop the result, then apply the change what you want.
$orders = Order::where('order_id', 2)
->leftJoin('test', 'test.order_id', '=', 'orders.order_id')
->get(['order.status, 'order.path', 'test.name']);
for($i = 0; i < orders.lenght();i++){
if(orders[i].status != 'COMPLETED'){
orders[i].path = null;
}
}

Linq Query Always returns False and Failed to Fetch Data

I am consuming wcf service into angular js application. I wrote linq query to check user information . If the is not null then query should catch the information and return true otherwise its should return false . But the problem is its always returns false and catch values null
Here is the linq Query .
public bool CreateCurrentAccountCheck(Current_Account_Holder_Details current_Account_Details)
{
using (HalifaxDatabaseEntities context =new HalifaxDatabaseEntities())
{
var query = (from x in context.Current_Account_Holder_Details
where x.Account_Holder_First_Name == current_Account_Details.Account_Holder_First_Name && x.Account_Holder_Last_Name == current_Account_Details.Account_Holder_Last_Name
&& x.Account_Holder_DOB == current_Account_Details.Account_Holder_DOB && x.Account_Holder_House_No == current_Account_Details.Account_Holder_House_No
&& x.Account_Holder_Street_Name == current_Account_Details.Account_Holder_Street_Name && x.Account_Holder_Post_Code == current_Account_Details.Account_Holder_Post_Code
select x).FirstOrDefault();
if (query!=null)
{
return true;
}
else
{
return false;
}
}
}
Here is the screen shot on debugging Mode .
The problem is pretty clear that there is no matched record with the where clauses. Also, you want to check the record and you should check it with using an unique id instead of other required or not required fields. It is the exact way to apply it. You should have some kind of unique AccountDetailId or other name which applies unique info for the records.
var query = (from x in context.Current_Account_Holder_Details
where x.AccountDetailId == current_Account_Details.AccountDetailId
select x).FirstOrDefault();

Resources