无法让 FullCalendar 工作 (Laravel 5)

Cannot get FullCalendar to work (Laravel 5)

我是 Laravel 5 的新手,我一直在尝试按照 Github 上的说明安装完整日历(声明于:https://github.com/maddhatter/laravel-fullcalendar

目前我只想在我的网站上显示日历,所以我做了以下操作:

通过 composer 安装了插件:'composer require maddhatter/laravel-fullcalendar' 并为我的 app.php 添加了一个别名。

接下来我制作了一个扩展 Eloquent\Model 的接口,如下所示:(当我将事件添加到我的数据库时):

<?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's id number
     *
     * @return int
     */
    public function getId();

    /**
     * Get the event's 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;
    }

}

然后在我的控制器中添加:

<?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'));

}

执行此操作后,说明上说我必须将以下内容添加到我的视图中(在我的文件夹中: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

我还在我的项目中做了“$bower install fullcalendar”来添加它们。

此外,这是我的文件夹结构,看到我可能遗漏了一些东西:

完成所有这些步骤后,它说应该显示日历,但我收到此错误:

可能是我太笨了,但我该如何解决这个问题?

您的控制器中的代码似乎不在方法中:

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'));
    }

}

更新:

您的 EventModelgetId() 方法也需要有正文(即:它需要有一个 opening/closing {},做点什么):

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

项目的 readme.md 缺少示例正文。

更新 2:

class EventModel extends Model \MaddHatter\LaravelFullcalendar\IdentifiableEvent

应该是

class EventModel extends Model implements \MaddHatter\LaravelFullcalendar\IdentifiableEvent

你最终成功了吗?

我只是在执行自己的过程中遇到了一些问题,我想我会添加我的经验来解决这个问题。

这对我在 fedora 上使用 laravel 5.1 和 mysql (mariadb) 很有效。

  1. 模型如下所示(从 https://github.com/maddhatter/laravel-fullcalendar 复制)并更改了来自
  2. 的以下行

class EventModel extends Eloquent implements \MaddHatter\LaravelFullcalendar\Event

class EventModel extends Model implements \MaddHatter\LaravelFullcalendar\Event

  1. 向控制器添加了以下行

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

  1. 对于数据库事件,在控制器中添加这个
$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. 如果您使用 blade 模板,我必须将脚本添加到我的主页和日历页面(如果从一个页面中删除,则不会显示日历)

  2. 脏易路由

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

我是 laravel 的新手,所以我知道可能有更简单的方法或者我没有遵循惯例,但这就是我到目前为止的工作方式,希望它有所帮助。