John Davidson

php - Laravel 5.8 | FullCalendar display events on calendar

0 comments
Message:


I am trying to display events on calendar from this package:


composer require maddhatter/laravel-fullcalendar

I did model:


class Event extends Model
{
protected $fillable = [
'title',
'start_date',
'end_date',
];
}

Event migrations with same columns. In controller I have function:


public function all()
{
$events = [];
$data = Event::all();

if($data->count()) {
foreach ($data as $key => $value) {
$events[] = Calendar::event(
$value->title,
true,
new \DateTime($value->start_date),
new \DateTime($value->end_date.' +1 day'),
null,
[
'color' => '#f05050',
]
);
}
}
$calendar = Calendar::addEvents($events);
return view('pages.block.event.all', compact('calendar'));
}

and in my all.blade.php:


{!! $calendar->calendar() !!}

the calendar is displayed correctly, but no events are applied to it. Could someone help me? Thank you



My Answer:
It looks like you are using the MaddHatter/laravel-fullcalendar package to display events on the calendar.

In your controller function, you are fetching all events from the database and creating Calendar events using the data. However, it seems like you are missing the step to actually render these events on the calendar.

You need to make sure that you are adding the events to the calendar object before passing it to the view. You can do this by calling the `addEvents` method on the calendar object and passing the events array to it.

Here is an updated version of your controller function:

php
public function all()
{
$events = [];
$data = Event::all();

if($data->count()) {
foreach ($data as $key => $value) {
$events[] = Calendar::event(
$value->title,
true,
new \DateTime($value->start_date),
new \DateTime($value->end_date.' +1 day'),
null,
[
'color' => '#f05050',
]
);
}
}

$calendar = \Calendar::addEvents($events);

return view('pages.block.event.all', compact('calendar'));
}


Make sure to include the `use MaddHatter\LaravelFullcalendar\Facades\Calendar;` at the top of your controller file.

Also, ensure that you have included the necessary CSS and JS files for FullCalendar in your layout file.

After making these changes, your events should be displayed on the calendar. Let me know if you need further assistance.

Rate this post

5 of 5 based on 8168 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us