'Issue with laravel eloquent model property

I'm trying to print {{$day->date}} in a view. Instead of full date YYYY-MM-DD the property only returns year YYYY.

Can anyone explain me why it is working like this ? & how to get full date from the property ?

The Code

@foreach($calendar as $day)
    {{dd($day->date)}}
    <br>
@endforeach

Expected output

2022-03-01
2022-03-02
...
...

Actual output

2022
2022
...
...

Attributes of $day in a view : using {{dd($day)}} blade

#attributes: array:4 [▼
    "date" => "2022-03-01"
    "day" => "Tuesday"
    "note" => "Public Holiday - Maha Sivarathri"
    "is_working_day" => 0
  ]

output of {{dd($day->date)}} in a view

2022

The Controller

$calendar = Calendar::wherebetween('date',[$date_from,$date_to])->get();
$data['calendar']=$calendar;
return view('cms.advanceprogram.calendar')->with($data);

The Calendar.php Model

class Calendar extends Model{
    use HasFactory;

    protected $primaryKey = 'date';

    public function advanceprograms(){
        return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
    }
}

The Migration

Schema::create('calendars', function (Blueprint $table) {
    $table->date('date')->primary();
    $table->string('day');
    $table->string('note')->nullable();
    $table->boolean('is_working_day');
});


Solution 1:[1]

I think, you shouldn't use date as primary key. Add column id for primary key.

Laravel cast primary key as integer. Replace or remove protected $primaryKey in model and you will get correctly result in Blade.

Model Calendar

class Calendar extends Model
{
    use HasFactory;

    public function advanceprograms(){
        return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
    }
}

View

@foreach($calendar as $day)
    {{$day->date}}
    <br>
@endforeach

Result:

2022-03-01

OR

Add to Calendar a line protected $keyType = 'string';

Like this:

class Calendar extends Model
{
    use HasFactory;

    protected $primaryKey = 'date';
    protected $keyType = 'string';

    public function advanceprograms(){
        return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
    }
}

OR

Add to Calendar a line protected $keyType = 'date'; Then field date will be instance of a Carbon class. Like this:

class Calendar extends Model
{
    use HasFactory;

    protected $primaryKey = 'date';
    protected $keyType = 'date';

    public function advanceprograms(){
        return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
    }
}

Then you may use Carbon methods in view. Like this:

@foreach($calendar as $day)
    {{$day->date->format('d-m-Y')}}
    <br>
@endforeach

Solution 2:[2]

You can try this

protected $primaryKey = 'date';
public $incrementing = false;   // no auto increment
public $timestamps = false;     // if there are not timestamp table

I think it's work

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Waad Mawlood