无法让 FullCalendar 工作(Laravel 5)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30662238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot get FullCalendar to work (Laravel 5)
提问by Pex
I'm quite new at Laravel 5 and I've been trying to follow the instructions on Github to install the full calendar (stated on: https://github.com/maddhatter/laravel-fullcalendar)
我是 Laravel 5 的新手,我一直在尝试按照 Github 上的说明安装完整日历(说明:https: //github.com/maddhatter/laravel-fullcalendar)
Currently I simply want to display the calendar on my site, so I did the following:
目前我只想在我的网站上显示日历,所以我做了以下事情:
Installed the plugin through composer with: 'composer require maddhatter/laravel-fullcalendar' & added an alias to my app.php.
通过 composer 安装了插件:'composer require maddhatter/laravel-fullcalendar' 并为我的 app.php 添加了一个别名。
Next I made a interface that extends Eloquent\Model like this: (For when I add Events to my database):
接下来,我创建了一个扩展 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;
}
}
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 doing this, is says on the instructions that I have to add the following to my view (which is in my folder: resources/pages/calendar.blade.php):
完成此操作后,说明中说我必须将以下内容添加到我的视图中(在我的文件夹中: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.
我还在我的项目中做了“$bower install fullcalendar”来添加它们。
Also, here is my folder structure, seeing I might have missed something:
另外,这是我的文件夹结构,看到我可能错过了一些东西:
After following all these steps, it says the calendar should display but I'm getting this error:
完成所有这些步骤后,它说日历应该显示,但我收到此错误:
It's probably me just being stupid, but how do I fix this?
可能我只是愚蠢,但我该如何解决这个问题?
采纳答案by davejal
Did you eventually get this to work?
你最终让这个工作了吗?
I just had some problems implementing myself and thought I'd add my experiences in resolving this.
我只是在实施自己时遇到了一些问题,并认为我会在解决此问题时添加我的经验。
This worked for me using laravel 5.1 and mysql (mariadb) on fedora.
这对我在 Fedora 上使用 laravel 5.1 和 mysql (mariadb) 有效。
- Model looks like the following (copied from https://github.com/maddhatter/laravel-fullcalendar) with changes to the following line from
- 模型如下所示(从https://github.com/maddhatter/laravel-fullcalendar复制),对以下行进行了更改
class EventModel extends Eloquent implements \MaddHatter\LaravelFullcalendar\Event
类 EventModel 扩展 Eloquent 实现 \MaddHatter\LaravelFullcalendar\Event
to
到
class EventModel extends Model implements \MaddHatter\LaravelFullcalendar\Event
类 EventModel 扩展模型实现 \MaddHatter\LaravelFullcalendar\Event
- Added the following lines to the controller
- 将以下行添加到控制器
use App\EventModel; use MaddHatter\LaravelFullcalendar\Event;
使用 App\EventModel; 使用 MaddHatter\LaravelFullcalendar\Event;
- 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 ); }
$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 ); }
If you work with blade templating I had to add the scripts to both my master and calendar pages (if removed from one it wouldn't show the calendar)
Dirty easy routing
如果您使用刀片模板,我必须将脚本添加到我的主页面和日历页面(如果从一个页面中删除它不会显示日历)
肮脏的简单路由
Route::resource('calendar','CalendarController');
Route::resource('calendar','CalendarController');
I'm new to laravel so I know there could be easier ways or I didn't follow conventions, but this is how I got it to work so far, hope it helps.
我是 laravel 的新手,所以我知道可能有更简单的方法,或者我没有遵循约定,但这就是我到目前为止让它工作的方式,希望它有所帮助。
回答by MaddHatter
It looks like the code in your controller isn't in a 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 EventModel
's getId()
method needs to have body as well (i.e.: it needs to have an opening/closing {
and }
, and do something):
你EventModel
的getId()
方法也需要有正文(即:它需要有一个开始/结束{
和}
,并做一些事情):
/**
* Get the event's id number
*
* @return int
*/
public function getId() {
return $this->id;
}
The readme.md for the project was missing an example body.
该项目的 readme.md 缺少示例正文。
Update 2:
更新 2:
class EventModel extends Model \MaddHatter\LaravelFullcalendar\IdentifiableEvent
should be
应该
class EventModel extends Model implements \MaddHatter\LaravelFullcalendar\IdentifiableEvent