1

I am using laravel 5.7.

I have a field on database set as below

$table->string('last_no', 5)

and I want to save the "last_no" will save digits as string. just like

Model::create( [ "last_no" => "00351"] );

but I don't know why it will save "351" to DB not "00351".

I have already try set as below but still have this issue.

protected $casts = [
    'last_no' => "string"
];

How can I fix it?

1 Answers1

0

you can do this with sprintf()

$last_no=sprintf('%05d',351);
Model::create( [ "last_no" => $last_no] );
Karan
  • 1,146
  • 1
  • 10
  • 24