Christianus Andre
Christianus Andre

Reputation: 237

Array in Controller laravel

Can anyone help me to fix my code ? i've the concept but dont know how to write them into code in controller

$temp=DB::table('temporary')->get();

    //dd($temp);
    if($temp['tahun'] < 'current year'){
        $temp['tahun'] = 'current year';
        $temp['nomor'] = 'nomor+1';
        $keluhan->no_laporan = $temp['nomor'];
    } else {
        $temp['nomor']++;
    }

Condition : my table temporary has 2 attribute tahun(value=2017 int) and nomor (value=1 int) i want to check if my tahun < this current year (real time year, idk the code so i write current year) then my tahun become = this curent year AND my nomor++ (+1 from its value first) how do i wrte them into laravel code ? please kindly help, thank you buddy

Upvotes: 1

Views: 352

Answers (2)

reza
reza

Reputation: 1507

DB::table('temporary')->->get() will return a collection. and from your code, it seems you are working with single column, you can use first().

For date can use simple date() function.

$temp=DB::table('temporary')->first();

//dd($temp);
if($temp->tahun < date("Y")){
    $temp->tahun = date("Y");
    $temp->nomor++;
    $keluhan->no_laporan = $temp->nomor;
} else {
    $temp->nomor++;
}

Upvotes: 2

Sanzeeb Aryal
Sanzeeb Aryal

Reputation: 3266

$temp=DB::table('temporary')->get(); will get a collection. so you have to loop. and Use Carbon to get the date.

foreach($temp as $temp)
{
  if($temp->tahun < Carbon\Carbon::now()->year){
    {
      $temp->tahun=Carbon\Carbon::now()->year;
      $temp->nomor=$temp->nomor+1;
      $temp->update();
     }
   else
    {
       $temp->nomor=$temp->nomor+1;
       $temp->update();
    }
}

Upvotes: 1

Related Questions