Issue
I have a custom unique increment in my program that i use in my controller, so each item wouldn't have same identifier
Controller
public function store(Request $request)
{
$request->validate([
'device_type_name' => 'required',
'device_brand_name' => 'required',
'device_status' => 'required',
]);
Transactions_in::getidtransactionsin();
$employees = new Transactions_in();
$employees->idDevice = "0";
$employees->DeviceType_id = $request->input('device_type_name');
$employees->DeviceBrand_id = $request->input('device_brand_name');
$employees->DeviceStatus_id = $request->input('device_status');
$employees->save();
$employees->update(['idDevice' => sprintf('NPC.ABC.%03d', $employees->id)]);
return redirect('/transactionsin')->with('success', 'New Item Added');
}
Model
class Transactions_in extends Model
{
protected $guarded = [];
public static function getidtransactionsin()
{
DB::table('transactions_ins')->orderBy('id', 'desc')->take(1)->get();
}
public function get_devicetypes()
{
return $this->belongsTo(DeviceType::class, 'DeviceType_id');
}
public function get_devicebrands()
{
return $this->belongsTo(DeviceBrand::class, 'DeviceBrand_id');
}
public function get_devicestatuses()
{
return $this->belongsTo(DeviceStatus::class, 'DeviceStatus_id');
}
}
Right now the idDevice
will be saved like this
| id | idDevice |
+----+---------------+
| 1 | NPC.ABC.001 |
| 2 | NPC.ABC.002 |
| 3 | NPC.ABC.003 |
Is there a way to add current year there so the output will be like this
| id | idDevice |
+----+---------------------+
| 1 | NPC.ABC.001.2019 |
| 2 | NPC.ABC.002.2019 |
| 3 | NPC.ABC.003.2019 |
And one more thing can i make it so in next year the idDevice
will start from 1 again.
| id | idDevice |
+----+---------------------+
| 1 | NPC.ABC.001.2019 |
| 2 | NPC.ABC.002.2019 |
| 3 | NPC.ABC.003.2019 |
| 4 | NPC.ABC.001.2020 |
| 5 | NPC.ABC.002.2020 |
| 6 | NPC.ABC.003.2020 |
| 7 | NPC.ABC.001.2021 |
| 8 | NPC.ABC.002.2021 |
| 9 | NPC.ABC.003.2021 |
Solution
Fist get latest record from database and increase id based on year
$latest_idDevice = Transactions_in::latest('id')->first();
if(isset($latest_idDevice->idDevice) && $latest_idDevice->idDevice!=null){
$arr = explode('.',$latest_idDevice->idDevice); // convert string to array
if($arr[3] == date('Y')){ //check that last record has current year
// increment id
$idDevice = 'NPC.ABC.'.($arr[2]+1).'.'.date('Y');
}
else { // last record belongs to previous year so add this year record with id 1
$idDevice = 'NPC.ABC.1.'.date('Y');
}
}
else { // there is no record in database so first record of year
$idDevice = 'NPC.ABC.1.'.date('Y');
}
This is your code for add record
$employees = new Transactions_in();
$employees->idDevice = $idDevice;
$employees->DeviceType_id = $request->input('device_type_name');
$employees->DeviceBrand_id = $request->input('device_brand_name');
$employees->DeviceStatus_id = $request->input('device_status');
$employees->save();
Answered By - Yasin Patel Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.