Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Heimdall's avatar

Laravel input old date

Hi,

I have problem with my input for recover the next data when i edit a form

When its not type="date" i use that :

value=" {{old('nom') ?? $chien-> name}}" }}>

but when its type date, that didn't work, so what its the problem ?

Thanks

0 likes
22 replies
Nakov's avatar

You should pass it as a second param what the default should be, like this:

{{ old('nom', $chien->name) }}

And btw, why you are sending nom but getting name from the model? Make sure your mistake is not there as well..

Heimdall's avatar

yes idk why i use nom ^^ but that work correctly, but for this input with type= "date" that dont work : {{old('naissance' ) that work correctly but that ?? $chien-> naissance}} dont work only when the input is date

<input type="date" class="form-control @error('naissance') is-invalid @enderror" name="naissance" placeholder="naissance" value=" {{old('naissance' ) ?? $chien-> naissance}}" }}>

Nakov's avatar

@heimdall then again why do you use ?? when you can pass it as a second parameter. I guess it does not work for your date because the format is not correct.. or you use JavaScript and it clears your input field.

Heimdall's avatar

Ye i change like you, old('naissance', $chien->naissance) but that dont work, if i edit my profil, this input dont recover the information on my database only because this type of input is "date"... Other input recover good information on my database

Nakov's avatar

As I said @heimdall the format might not be the same, for example in your database if the format is Y-m-d and in your input field the format is d-m-Y for example, you will need to manually format your date per your date field, for example:

<input type="date" class="form-control @error('naissance') is-invalid @enderror" name="naissance" placeholder="naissance" value=" {{old('naissance', optional($chien->naissance)->format('d-m-Y') ) }}" }}>

Play around with it, as I don't have any of your data to tell you exactly what the problem is..

Heimdall's avatar

Yes i think is that the problem, my database is Y-m-d and my input is d-m-Y but i try to use your code but that change nothing ;/

Nakov's avatar

@heimdall I cannot test not having your code. You should learn to debug. You can do it step by step. Make sure for example in the controller what does the this returns:

dd($chien->naissance);

Of course you should put this code after the $chien is being initialized. Is it empty what is it? Then try this:

dd($chien->naissance->format('d-m-Y'));

Make sure that it returns a string with the correct date format.

Do you use any JavaScript for your date field? Make sure that that does not clears your input field..

Heimdall's avatar

dd($chien->naissance); for that the response is 2019/10/14

and for that dd($chien->naissance->format('d-m-Y')); i have one error : Call to a member function format() on string

and no i dont use JS

Nakov's avatar

@heimdall in your model Chien add this

protected $dates = ['naissance'];

this will make the naissance an instance of a Carbon so you can then use format on it.

Heimdall's avatar

ok ty so now the response is good :

"25-10-2019"

Nakov's avatar

@heimdall if it works you should consider marking the answer as "Best Answer".

Heimdall's avatar

yes but dd($chien->naissance->format('d-m-Y')); is on Controller if i want update my database i needto change my code but my model for add data is:

$naissance = request("naissance"); $chien -> naissance = $naissance; $chien -> update();

but how add format('d-m-Y') ? if i use that : $chien -> naissance = $naissance -> format('d-m-Y') ; i have the next error :

Call to a member function format() on string

Nakov's avatar

@heimdall for update you don't need to format the code but you need to create a Carbon object

$naissance = request("naissance"); 
$chien->naissance = Carbon\Carbon::parse($naissance); 
$chien->update();
Heimdall's avatar

namespace App\Http\Controllers; use Carbon\Carbon; use App\Chien;

i add Carbon\Carbon; but i have this error now :

Class 'Carbon\Carbon\Carbon' not found

Nakov's avatar

omg.. if you already have an import of Carbon, then just use Carbon::parse(..

Heimdall's avatar

yes sorry, but this code : $chien->naissance = Carbon\Carbon::parse($naissance); dont choose my format for the date ?

dd( $chien-> naissance = Carbon::parse($naissance));

and the response is Carbon @1570060800 {#328 date: 2019-10-03 00:00:00.0 UTC (+00:00) }

Heimdall's avatar

ok.. so you dont know why the date after Carbon::parse is Y-m-d ? and not d-m-Y ?

Nakov's avatar

@heimdall I know, that's because that's the default format, and that's the format that the Database wants. So when you need to display it, then you format into the wanted format.

Heimdall's avatar

and i cant change the default format on database ?

Heimdall's avatar

ok ty for your time and sorry for my bad english, i m french student and i start to learn Laravel

Please or to participate in this conversation.