I had a question about creating an array based on the number that was requested.
For example, if the number requested is ‘5’, it will create a sequence in the array like this [001,002,003,004,005)
I’m using Laravel, I’ve been trying something like this and it doesn’t work.
foreach($request->jumlah as $jumlah) {
$storedetail = new DetailAlat();
$storedetail->id_alat = $storealat->id_alat;
$storedetail->sub_id_alat = $jumlah;
$storedetail->created_by = Session::get('username');
$storedetail->save();
}
I hope someone in here can help me
The error is:
Call to a member function toArray() on string
2
Answers
You can use the
range()
function to create an array with a sequence of numbers. However, to get the numbers in the format you want (001, 002, 003, etc.), you can usearray_map()
function withsprintf()
to format the numbers.Here is a simplified version
But for this to work you need to define $fillable property