1

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

1
  • what is the variable $keluhan in the code? Commented Mar 31, 2017 at 2:09

2 Answers 2

2

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++;
}
Sign up to request clarification or add additional context in comments.

Comments

1

$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();
    }
}

5 Comments

Loading Carbon just to get the current year seems excessive when you can just use date('Y').
@fubar Carbon is a built in feature of laravel and is much easier and semantic. with date() we have to deal with strtotime, formatting issues, lots of calculations, and more. and
Carbon is a dependency in Laravel, it's not built in. And to use date('Y') to get the current year you do not need to use strtotime, nor make any calculations whatsoever.
Ok. thank you for the information. I'll learn and make sure of it.
Thank you guys for helping its now work, great. God Bless You.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.