Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A two digit month could not be found Data missing in Carbon in Laravel

I'm building a small application on Laravel 5.4 I'm trying to receive dates from a datepicker widget from front end and parsing it into Carbon date format something like this:

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

In continuation with my previous questions: How to format date receiving through Vuejs Datepicker in laravel , I successfully added this to my database, and while calling it I'm placing an accessor in my model and trying to fetch the date in diffForHumans() format which was my previous question: A two digit month could not be found Data missing in Laravel , this works perfectly fine whenever I'm fetching the model, until I'm not assigning this schedule attribute to any other variable, Now while retrieving the models in the controller and assigning to the a value with something 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'm getting the same error:

A two digit month could not be found Data missing

And the same works perfectly fine if I do:

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 with the name: Interaction I'm defining my attribute something like this:

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

EDIT

FYI: I'm having my InteractionSummary model and have following relationship:

public function interaction()
{
    return $this->belongsTo('App\Interaction');
}
like image 209
Nitish Kumar Avatar asked Jul 07 '17 05:07

Nitish Kumar


2 Answers

Most certainly your problem is caused by this line:

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

The reason is, that you have setup date columns (see linked question) like this:

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

So 'schedule' is treated as a date.

When you retrieve your schedule date via $meeting->schedule The value is modified by your mutator and ends up with something like '1 hour ago'.

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

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

As you marked 'schedule' as a date in your dates array Laravel is trying to parse it with Carbon. It expects to have dates in the format that is specified in your model's $dateFormat attribute (defaulting to Y-m-d H:i:s).

like image 182
shock_gone_wild Avatar answered Nov 03 '22 07:11

shock_gone_wild


As @shock_gone_wild said, if you have 'schedule' inside your $dates array,

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

Laravel will give you a Carbon\Carbon instance.

If that's your case, you should format before sending it (otherwise it will be a Carbon object).

$tempData->schedule = $meeting->schedule->format('Y-m-d');
like image 29
Renato Gomes Avatar answered Nov 03 '22 08:11

Renato Gomes