Closed lstables closed 10 years ago
Hey, I don't know what your table looks like, but you need to get your events from your table, and process the collection into a new array that is grouped by date. The timestamp has to be the key. I use this helper function to group them by date.
function array_group_by($value, $items) {
$groupedArray = array();
foreach ($items as $key => $values) {
$item = $values[$value];
$groupedArray[$item][] = $values;
}
return $groupedArray;
}
So, something like this:
$events = Event::get()->toArray();
$events = array_group_by('event_date', $events);
Assuming you table is something like:
id > int
title > varchar
...
event_date > timestamp
Hope this helps,
Thanks!
My table is
id > int
title > varchar
date > varchar as Im using datepicker with specific date format.
But Im getting Call to undefined function array_group_by()
That table should work, you need to include that function in your project cause it is not part of laravel. The code for array_group_by is in my above post.
Yes I moved the array_group_by to a helpers.php file and now I get Array to string conversion error
Important, You need to format your date back to Y-m-d H:i:s, if not it will fail. You should use a timestamp, and format your date for your view (in the required format). And reformat it for saving. Is the helper file loaded? You can load it in
app/start/global.php at the bottom
require app_path().'/helpers.php';
I would not recommend storing dates as varchar.
Yes i understand that and just changing my code, bear with me.
So I changed my code to insert a timestamp.
I now have id > int title -> varchar date > datetime
But still get array to string conversion error
And my helpers.php is loaded as I use it for some other functions.
Hard to tell without looking at the error/code... You need to pass an array to the array_group_by, not a collection.
That is the only thing i can think of without looking at your code :S
$events = Events::get()->toArray();
$events = array_group_by('date', $events);
$cal = Calendar::make();
/**** OPTIONAL METHODS ****/
$cal->setDate(Input::get('cdate')); //Set starting date
$cal->setBasePath('admin/employees/edit/'); // Base path for navigation URLs
$cal->showNav(true); // Show or hide navigation
$cal->setView(Input::get('cv')); //'day' or 'week' or null
$cal->setStartEndHours(8,20); // Set the hour range for day and week view
$cal->setTimeClass('ctime'); //Class Name for times column on day and week views
$cal->setEventsWrap(array('<p>', '</p>')); // Set the event's content wrapper
$cal->setDayWrap(array('<div>','</div>')); //Set the day's number wrapper
$cal->setNextIcon('<i class="fa fa-chevron-right"></i>'); //Can also be html: <i class='fa fa-chevron-right'></i>
$cal->setPrevIcon('<i class="fa fa-chevron-left"></i>'); // Same as above
$cal->setDayLabels(array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')); //Label names for week days
$cal->setMonthLabels(array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')); //Month names
$cal->setDateWrap(array('<div>','</div>')); //Set cell inner content wrapper
$cal->setTableClass('table'); //Set the table's class name
$cal->setHeadClass('table-header'); //Set top header's class name
$cal->setNextClass('btn'); // Set next btn class name
$cal->setPrevClass('btn'); // Set Prev btn class name
$cal->setEvents($events); // Receives the events array
/**** END OPTIONAL METHODS ****/
$calendar = $cal->generate();
return View::make('employees.edit', compact('employee','title','calendar'));
that looks good to me. can you var_dump the $events? the 1st one
dd($events); produces -
array (size=1)
0 =>
array (size=3)
'id' => string '6' (length=1)
'title' => string 'Test Event' (length=10)
'date' => string '2014-06-04 00:00:00' (length=19)
looks good, you can paste code here: laravel.io/bin
Controller http://laravel.io/bin/4PM9D
Model - http://laravel.io/bin/1WeNP
everything looks good, try this:
....
$calendar = $cal->generate();
return $calendar;
if you see the calendar, then your error is in the view.
Nope, still same error plus I need to pass the $calendar in the view
can you dd the $events the 2nd time (when grouped)?
$events = Events::get()->toArray();
$events = array_group_by('date', $events);
dd($events);
Produces:
array (size=1) '2014-06-04 00:00:00' => array (size=1) 0 => array (size=3) 'id' => string '6' (length=1) 'title' => string 'Test Event' (length=10) 'date' => string '2014-06-04 00:00:00' (length=19)
That is your problem, take note of the required format of the array passed:
"2014-04-09 10:30:00" => array( #KEY TIMESTAMP
"Event 1", #STRING
"Event 2 <strong> with html</stong>", #STRING
),
You need to further process your $events.
It has to be like this, because eveyone's table structure will be different.
Sorry I dont follow you, what do you mean?
I changed this $events = array_group_by('date', $events); to $events = array_group_by('title', $events);
That removes the error, but nothing is showing up on today's date as an event.
Any idea's?
one sec, i almost have an example for you
Brilliant!
This should work:
$events = Events::get()->toArray();
$events = array_group_by('date', $events);
$new_events = [];
foreach ($events as $key => $value) {
foreach ($value as $v) {
$new_events[$key][] = $v['title'];
}
}
//CALENDAR CODE
//...
$cal->setEvents($new_events);
$calendar = $cal->generate();
Yep worked a treat, legend thank you! A great package this is, I would suggest you add some more documentation for people like me maybe use that as an example too.
Thanks again :)
awesome!
How can I loop through a events table to show the events within the calendar.
In the docs you have:
But I want to loop through a model called Events
so ideally I would setup $events = Events::all(); then do a foreach for this, but how would one do it and add it the array above so these dates/events then show within the calendar?