How can I check if an insert executed? - laravel

How can I check if an insert executed?
$s = Visitor::insert($items);
Variable $scontains the inserted object, not a boolean type.

$s->exists will tell you if the entry is in the database or not.

Another good check is to use
$s = Visitor::insertGetId($items);
if($s)
{
// inserted
}

another method is to use wasRecentlyCreated:
$s = Visitor::insert($items);
if ($s->wasRecentlyCreated === true) { //check if a new row was created
return 'true';
}

Related

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
}

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

Laravel Search Condition Problem. When condition is value empty, then go to else condition. But it's not work properly

This is my search code please see then solution, thank you.
You have to use like this
$report_ex = Expenses::where(condition)->where(condition)->get();
or
$report_ex = Expenses::where(condition)->where(condition)->first();
If you are using
$report_ex = Expenses::where(condition)->where(condition)->first();
then you need to call
if(!empty($report_ex))
{
// your code
}
else
{
//your code
}
But if you are using
$report_ex = Expenses::where(condition)->where(condition)->get();
then you should use
if(count($report_ex) > 0)
{
// your code
}
else
{
// your code
}
since get function returns an empty object

error foreach in View Codeigniter : Illegal string offset

Please help, i use foreach to return data from database, but it only return the last data (array always update with last data). So i modified the code but i got error : Illegal string offset in foreach.
here is my controller
foreach($dataDesa as $desa)
{
$namaDesa = json_decode($this->curl->simple_get($desa->alamat_api.'/info_desa?ID_DESA=1'));
$datakonten[$namaDesa] = array(
'proyek' =>json_decode($this->curl->simple_get($desa->alamat_api.'/proyek_pertanian')),
'nama_desa' =>json_decode($this->curl->simple_get($desa->alamat_api.'/info_desa?ID_DESA=1')),
'lelang' =>json_decode($this->curl->simple_get($desa->alamat_api.'/pelelangan'))
);
}
$datakonten['getAllDesa'] = $this->M_desa->getAllDesa($idStatus);
$nama_Desa = array();
foreach($datakonten['getAllDesa'] as $row)
{
$nama_Desa[] = $row->nama_desa;
}
$datakonten['nama_desa']=$nama_Desa;
$data['content'] = $this->load->view('public/konten/v_home',$datakonten,TRUE);
$this->load->view('public/halaman/v_home',$data);
and here is my view
$i=0;
foreach($nama_desa[$i]['proyek'] as $rows)
{
$nama = $rows->nama_proyek;
i++;
}
i've tested $nama_desa[0] and $nama_desa[1] and they have value returned (the value is "Kranon" and "Wulung") and i use the value like $Kranon['proyek'] and theres no error and returned the value that i want, but when i combined it with $nama_desa[$i]['proyek'] i got this error.
`
Please help, thanks in advance
You only push $row->nama_desa to $nama_desa. and try to get ['proyek'] from $nama_desa?
As you explained above, you getting value with this $nama_desa[1] but you trying to access this key $nama_desa[$i]['proyek'] which means its looking for $nama_desa[1]['proyek'] which is not exist.
Just do
print_r($name_desa);die;
You will find there is no key present with the name of 'proyek' on $name_desa[1] (key '1' i have only given for example. It can be any number of key)
I guess you will get your value by accessing this $nama_desa['proyek'] OR You will get idea with print result.
I have just found my solution.
I always getting an error whenever i want to pass variable that refers another value into foreach, so I modified my controller so I can pass the value into foreach in View directly. And I moved kind of "get-data-from-API" from Controller into View.
My Controller :
$datakonten['getAllDesa'] = $this->M_desa->getAllDesa($idStatus);
$data['content'] = $this->load>view('public/konten/v_home',$datakonten,TRUE);
My View :
foreach($getAllDesa as $rows)
{
$proyek = json_decode($this->curl->simple_get($rows->alamat_api_lelang));
foreach($proyek as $baris)
{
$namaProyek = $baris->nama_proyek;
?>
Nama Proyek : <?php echo $namaProyek;?></br>
<?php
}
}

Symfony2: check whether session exists or not

Is there a method to check whether a session exists or not?
I tried this method, but it always gives me a 'Bravo !' answer:
$session = $this->getRequest()->hasPreviousSession();
if($session)
{
return new Response('Bravo !');
}
else
{
return new Response('Oooops !');
}
$this->container->get('session')->isStarted()
is what you're looking for.

Resources