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'); }