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

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

Related

gettin request/query parameters with less code

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;
}

How to update only one property of model?

I'm working on user consents. In my request I have these properties:
$newsLetters (bool|nullable),
$sms (bool|nullable),
$billEmail (bool|nullable),
I need update only one. So I need to find one which is not null and update it, if in my request is more than one properties with bool values i need to throw exception.
How can I achieve this?
My request extends spatie/laravel-data.
I don't understand why would you handle something like this on the backend (you can use radio button for this and always send only one value), you can use validation for requests or something like this:
$newsLetters = null;
$sms = true;
$billEmail = null;
$values = [$newsLetters, $sms, $billEmail];
$filter = sizeof(array_filter($values, function($el) { return $el === null;})) < 2;
if($filter) {
//return exception or whatever
} else {
//update values
}

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?

I want to retrieve the id from the query result and reuse it for the next query laravel

I want to use the id from the select query results, then I want to use that id again to find data using wherein. but the error i found,
public function getMaut(){
$merek="samsung";
$tipe="led TV";
$display ="25-32";
$kriteria=Kriteria::all();
$alternatif = Alternatif::where('merek',$merek)->where('tipe',$tipe)->where('display',$display)->get();
$alternatif_id = (array)$alternatif->id;
$nilaialter = Nilaialternatif::whereIn('id_alternatifs',$alternatif_id);
return view('spk.index',compact('kriteria','alternatif','nilaialter','alternatif_id'));
}
Use the pluck method to retrieve all values for a given key.
$alternatif = Alternatif::where('merek', $merek)->where('tipe', $tipe)->where('display', $display)->get();
$alternatif_id = $alternatif->pluck('id');
$nilaialter = Nilaialternatif::whereIn('id_alternatifs', $alternatif_id)->get();
Alternatively, you can create a hasMany relation to 'Nilaialternatif' at 'Alternatif' model.
class Alternatif extends Model
{
public function nilaialternatifs()
{
return $this->hasMany(Nilaialternatif::class, 'id_alternatifs');
}
}
Then query the relationship like
$alternatif = Alternatif::with('nilaialternatifs')->where('merek', $merek)->where('tipe', $tipe)->where('display', $display)->get();
$nilaialter = $alternatif->nilaialternatifs;
$nilaialter = [];
$alternatif = Alternatif::where('merek', $merek)->where('tipe', $tipe)->where('display', $display)->get()->toArray();
if (count($alternatif)) {
$alternatif_ids = collect($alternatif)->pluck('id')->toArray();
$nilaialter = Nilaialternatif::whereIn('id_alternatifs', $alternatif_ids)->get()->toArray();
}
use laravel collection, https://laravel.com/docs/6.x/collections
use pluck method of collection,https://laravel.com/docs/6.x/collections#method-pluck

How do I return records by using chunk()?

I'm using chunk() to find a value from the database based on the question. But how do I return the entityFound[] array?
$chunkRecord = $this->company()->entityValues()->chunk(5,
function ($entityValues) use ($entityFound, $question) {
foreach ($entityValues as $entityValue)
if (str_contains($question, $entityValue->name))
$entityFound[] = $entityValue->name;
return $entityFound;
});
// I want to run this array and do something else.
dd($entityFound);

Resources