Inserting Date Value into Date Field Using Laravel

Inserting Date Value into Date Field using Laravel

When using <input type="date">, the value will need to be formatted in ISO 8601 date format: YYYY-MM-DD

Try mutating your $shipment->date value by appending ->format('Y-m-d') and it should match the expected format.

Give this a shot:

    <input 
...
value="{{ $shipment->date->format('Y-m-d') }}"
...
>

You can learn a bit more about it here:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date
https://en.wikipedia.org/wiki/ISO_8601

Insert Date and time in dataBase using Laravel 5.4

Why you want to store it in a custom format which is not defined in your database? Wrong idea in my opinion.


Proper solution according to me:

I'll make an assumption that you are storing the date and time in MySQL database as TimeStamp value with a default value of null. Something like this:

$table->timestamp('your-date-column')->nullable();

When hitting the controller method:

public function yourControllerMethod(Request $request)
{
// Don't forget to import the Carbon namespace
$dateTime = Carbon::parse($request->your_datetime_field);

$request['your_datetime_field'] = $dateTime->format('Y-m-d H:i:s');
}

When retrieving the your_datetime_field, parse it as Carbon instance by default using like this:

Test.php

/**
* The dates that will be mutated to Carbon instance.
*
* @return array
*/
protected $dates = [
'your_datetime_field',
];

Now that you have converted the field into Carbon instance on runtime, you are free to edit the date and time format as per your requirement.


Not a proper solution:

Warning, I have not tested this, but I guess it should work..

If you still want to save it as a custom format in the database field, save it as a string format with a default value of null.

$table->string('your-date-column')->nullable();

And then cast it to datetime type at the runtime.

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'your_datetime_field' => 'datetime:Y-m-d',
];

Again, I would recommend not to go in the wrong direction unless you know what you are doing and what are the consequences.

You get the idea.

Inserting date value into database Laravel

The rule of edu_start is array, so when you want to validate date you should set edu_start array items not the edu_start.

Please try to change your rules to:

public function rules()
{
return [
'edu_organisation' => ['required','array'],
'edu_start' => ['required','array'],
'edu_start.*' => ['date'],
'edu_end' => ['required','array'],
'edu_end.*' => ['date'],
'edu_position' => ['required','array'],
'edu_description' => ['required','array']
];
}

How to insert date in laravel Controller?

//
$validator = Validator::make($request->all(), [
'startdate' => 'required|date_format:d/m/Y',
'enddate' => 'required|date_format:d/m/Y',
'reason' => 'required|min:20',
]);

if ($validator->fails()) {
return response()->json($validator->errors(), 422);
}
$date_start = $request->input('startdate');
$date2_start = DateTime::createFromFormat('d/m/Y', $date_start);
$date3_start = $date2_start->format("Y-m-d");
$date_end = $request->input('enddate');
$date2_end = DateTime::createFromFormat('d/m/Y', $date_end);
$date3_ens = $date2_end->format("Y-m-d");

$leave = new Leavemodel();
$leave->leave_startdate = $date3_start;
$leave->leave_enddate = $date3_ens;
$leave->leave_notify = 0;
$leave->leave_reasone = $request->input('reason');
$leave->save();

How to insert date format YY-MM into database?

@Ted Stresen-Reuter

Sorry for the late reply couldn't find a solution that works within the model. tried your method which i have tried before with minor changes.

thanks a lot for trying to help me.. i found a method to complete this inside the controller which i will not recommend, but this was the best i was able to find which works and i was on a tight schedule.

'date_from' => isset($date_from[$key]) ? date('Y-m-d h:i:s', strtotime($date_from[$key])) : '',
'date_to' => isset($date_to[$key]) ? date('Y-m-d h:i:s', strtotime($date_to[$key])) : '',

the type timestamp accepts at least dates in full formats. as i know how i pass the data from blade to the controller i was able to add a date method and within it a strttotime method to convert my YY-MM into YY-MM-DD and the date that is inserted by default will be 01.

How to get the date and insert it into php with Laravel

Laravel comes with this functionality out of the box.

You can add this code to your migration:

$table->timestamps();

This will create a created_at and updated_at field in your database which are automatically filled.


Or if you want to don't want to do it within Laravel, you can do one of these:

  • Set the default value of the column to: TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  • Assign the value in the controller: $model->created_at = date('Y-m-d'); and save it normally.

Insert date format in dd/mm/yyyy through laravel Eloquent Model

It seems you are trying to store or retrieve the date value in specific format
the database accept the data in Y-m-d format.i.e(yyyy-mm-dd)
I would suggest you to Defining An Accessor for the respective model.
E.g

public function getCreatedAtAttribute($value)
{
$date = Carbon::parse($value);
return $date->format('d-m-Y');
}

Note:
use Carbon\Carbon;

use this in your model file before the class begins.

Insert the current date time using Laravel

Use the Laravel helper function

now()

Otherwise, use the carbon class:

Carbon\Carbon::now()

It is used like this:

$data = array("name" => $name,"address" => $address,"created_at"=> Carbon::now(),"updated_at"=> now());
DB::table('student')->insert($data);

For more information, see now()

Laravel insert date created_at from 1 to 30

you need to set timestamps = false;

$test->timestamps = false



Related Topics



Leave a reply



Submit