Could not find a two-digit month. Data not available in Carbon in Laravel

I am creating a small application on Laravel 5.4 . I'm trying to get dates from a datepicker widget from the front end and parse it in a Carbon date format like this:

 Carbon\Carbon::parse($request->schedule)->toDateTimeString(); 

In continuation of my previous questions: How to format the receipt of the date through Vuejs Datepicker in laravel , I successfully added this to my database, and calling it, I put the accessory in my model and trying to get the date in diffForHumans() format, which was my previous Question: Failed to find a two-digit month. There is no data in Laravel , this works great whenever I retrieve a model until I assign this schedule attribute to some other variable. Now, getting the models in the controller and assigning the value of a like this:

 public function getData() { $user = Auth::user(); $summaries = InteractionSummary::all(); $meetings = []; foreach($summaries as $summary) { $company = []; $tempData = $summary->interaction->where('user_id', $user->id)->get()->first(); if($tempData) { $meeting = Interaction::find($tempData->id); $tempData->schedule = $meeting->schedule; $meetings[] = $tempData; } } return response()->json(['meetings' => $meetings], 200); } 

I get the same error:

Could not find a two-digit month. Missing data.

And the same thing works great if I do this:

 public function getFutureData() { $user = Auth::user(); $currentTime = Carbon::now(); $interactions = $user->interaction->where('schedule', '>=', $currentTime); $meetings = []; foreach($interactions as $interaction) { $interaction->meeting = $meeting; $meetings[] = $interaction; } return response()->json(['interactions' => $meetings], 200); } 

In my model named: Interaction I define my attribute something like this:

 public function getScheduleAttribute($value) { return Carbon::parse($value)->diffForHumans(); } 

EDIT

FYI: I have an InteractionSummary model and have the following relationship:

 public function interaction() { return $this->belongsTo('App\Interaction'); } 
+5
source share
3 answers

Most likely your problem is caused by this line:

 $tempData->schedule = $meeting->schedule; 

The reason is that you have the installation date columns installed (see related question) as follows:

 protected $dates = [ 'schedule', 'created_at', 'updated_at', 'deleted_at' ]; 

So, the "schedule" is considered as a date.

When you get the schedule date via $ meeting-> schedule, the value is changed by your mutator and ends with something like "1 hour ago."

So with $ tempData-> schedule = $ meeting-> schedule; you are actually trying to set an invalid date for $ tempData-> schedule. It will translate as

 $tempData->schedule = '1 hour ago'; 

As you noted the โ€œscheduleโ€ as the date in your date, the Laravel array tries to parse it using Carbon. It expects to have dates in the format specified in your $ dateFormat attribute of the model (default is Ymd H: i: s).

+4
source

As @shock_gone_wild said, if you have a โ€œscheduleโ€ inside your $ dates array,

 protected $dates = [ 'schedule', 'created_at', 'updated_at', 'deleted_at' ]; 

Laravel will give you a copy of Carbon \ Carbon.

If this is your business, you must format it before sending it (otherwise it will be a Carbon object).

 $tempData->schedule = $meeting->schedule->format('Ym-d'); 
+1
source

Try the following:

 public function getData() { $user = Auth::user(); $summaries = InteractionSummary::all(); $meetings = []; foreach($summaries as $summary) { $company = []; $tempData = $summary->interaction->where('user_id', $user->id)->get()->first(); if($tempData) { $meetings[] = $tempData; } } return response()->json(['meetings' => $meetings], 200); 
0
source

Source: https://habr.com/ru/post/1269585/


All Articles