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

3

5

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');
}
Slumlord answered 7/7, 2017 at 5:19 Comment(4)
Why are you using get() and then first() in first part of your code ? and can you describe your error in more detail.Booma
@SagarGautam I'm having a relation in InteractionSummary model as: public function interaction() { return $this->belongsTo('App\Interaction'); }Slumlord
You might have default null values to schedule column, that might be a reason for your error.Booma
@SagarGautam Nopes, I've no null value, infact it is the required field.Slumlord
D
5

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).

Dietsche answered 21/7, 2017 at 7:19 Comment(0)
A
1

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');
Aliment answered 23/7, 2017 at 14:3 Comment(1)
also you can take original non mutated value $tempData->schedule = $meeting->getOriginal('schedule')Bose
A
0

Try 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)
       {
           $meetings[] = $tempData;
       }
   }
   return response()->json(['meetings' => $meetings], 200);
Ante answered 26/7, 2017 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.