Can't get FullCalendar to work (Laravel 5)

I was brand new in Laravel 5 and I was trying to follow the instructions on Github to set the full calendar (pointed to: https://github.com/maddhatter/laravel-fullcalendar )

Currently I just want to display the calendar on my site, so I did the following:

Installed the plugin via composer with: 'composer require maddhatter / laravel-fullcalendar' and added an alias to my app.php.

Next, I created an interface that extends Eloquent \ Model as follows: (When I add Events to my database):

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class EventModel extends Model \MaddHatter\LaravelFullcalendar\IdentifiableEvent{

    //fillable = Being able to mass assign (MassAssignment exception)

    protected $dates = ['start', 'end'];
    /**
     * Get the event id number
     *
     * @return int
     */
    public function getId();

    /**
     * Get the event title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Is it an all day event?
     *
     * @return bool
     */
    public function isAllDay()
    {
        return (bool)$this->all_day;
    }

    /**
     * Get the start time
     *
     * @return DateTime
     */
    public function getStart()
    {
        return $this->start;
    }

    /**
     * Get the end time
     *
     * @return DateTime
     */
    public function getEnd()
    {
        return $this->end;
    }

}

      

Then in my controller, I added:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class CalendarController extends Controller {

$events[] = \Calendar::event(
    'Event One', //event title
    false, //full day event?
    '2015-06-05T0800', //start time (you can also use Carbon instead of DateTime)
    '2015-06-05T0800', //end time (you can also use Carbon instead of DateTime)
    0 //optionally, you can specify an event ID
);

$eloquentEvent = EventModel::first(); //EventModel implements MaddHatter\LaravelFullcalendar\Event  

$calendar = \Calendar::addEvents($events) //add an array with addEvents
    ->addEvent($eloquentEvent, [ //set custom color fo this event
        'color' => '#800',
    ])->setOptions([ //set fullcalendar options
        'firstDay' => 1
    ])->setCallbacks([ //set fullcalendar callback options (will not be JSON encoded)
        'viewRender' => 'function() {alert("Callbacks!");}'
]); 

return view('pages.calendar', compact('calendar'));

}

      

After that it says the instructions that I should add to my view (which is located in my folder: resources / pages / calendar.blade.php):

@extends('masterpage')
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.css"/>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.print.css"/>


@section('content')

<h1> Calendar </h1>
{!! $calendar->calendar() !!}
{!! $calendar->script() !!}
@stop

      

I also did "$ bower install fullcalendar" in my project to add them.

Also, here is my folder structure, seeing that I was missing something: folder structure

After following all these steps, it says the calendar should be displayed, but I am getting this error: Undefined variable: calendar

Maybe I'm just stupid, but how do I fix this?

+3


source to share


2 answers


Did you end up getting it from work?

I just had problems realizing myself and thought that I would add my own experience in solving this issue.

This worked for me using laravel 5.1 and mysql (mariadb) on Fedora.

EventModel class extends Ink Tools \ MaddHatter \ LaravelFullcalendar \ Event

to

class EventModel extends model \ MaddHatter \ LaravelFullcalendar \ Event



  1. Added the following lines to the controller

use App \ EventModel; use MaddHatter \ LaravelFullcalendar \ Event;

  1. For db events add this inside the controller
$event = EventModel::all();

foreach ($event as $eve) {
  $events[] = \Calendar::event(
  $eve->title, //event title
  $eve->allDay, //full day event?
  $eve->start, //start time (you can also use Carbon instead of DateTime)
  $eve->end, //end time (you can also use Carbon instead of DateTime)
  $eve->id //optionally, you can specify an event ID
  );
}

      

  1. If you are working with a click template I had to add scripts to my master and calendar pages (if removed from one, this will not show the calendar)

  2. Dirty simple routing

Route :: resource ('calendar', 'CalendarController');

I'm new to laravel so I know there might be easier ways, or I haven't followed conventions, but this is how I waited for it to work, hope it helps.

+3


source


It looks like your controller code is not in the method:

class CalendarController extends Controller {

    public function index() { //code should be inside a method
        $events[] = \Calendar::event(
            'Event One', //event title
            false, //full day event?
            '2015-06-05T0800', //start time (you can also use Carbon instead of DateTime)
            '2015-06-05T0800', //end time (you can also use Carbon instead of DateTime)
            0 //optionally, you can specify an event ID
        );

        $eloquentEvent = EventModel::first(); //EventModel implements MaddHatter\LaravelFullcalendar\Event  

        $calendar = \Calendar::addEvents($events) //add an array with addEvents
            ->setOptions([ //set fullcalendar options
                'firstDay' => 1
            ])->setCallbacks([ //set fullcalendar callback options (will not be JSON encoded)
                'viewRender' => 'function() {alert("Callbacks!");}'
        ]); 

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

}

      

Update:

Your method EventModel

getId()

should also have a body (i.e. it should have an open / close {

and }

and and do something):

/**
 * Get the event id number
 *
 * @return int
 */
public function getId() {
    return $this->id;
}

      



The readme.md for the project was missing an example body.

Update 2:

class EventModel extends Model \MaddHatter\LaravelFullcalendar\IdentifiableEvent

      

it should be

class EventModel extends Model implements \MaddHatter\LaravelFullcalendar\IdentifiableEvent

      

+1


source







All Articles