27 Aug

Working with mySQL datetime, Bootstrap DateTimePicker and Laravel 4

I had some trouble formatting my dates for insertion and properly populating them back into my Laravel 4 blade. Here’s my solution:

within the form on edit.blade.php – note the $block->formatDateTime(Form::getValueAttribute(“date_start”)) – this passes the date_start value to the formatDateTime function in Block.php (below)

<div class="form-group">
  <div class="input-group date" id="startDate">
    {{ Form::label('date_start', 'Date/Time Begins') }}
    {{ Form::text("date_start", $block->formatDateTime(Form::getValueAttribute("date_start")), array("placeholder" => "2014-09-09 12:00:00", 'class' => 'form-control')) }}                
    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>

Function within app/models/Block.php – this formats the datetime from the database to display properly in the form so that the Bootstrap DateTimePicker can handle it

	public function formatDateTime($d) {
		$olddate = strtotime( $d );
		//$newdate = date( 'Y-m-d H:i:s', $olddate );
		$newdate = date( 'd/m/Y H:i A', $olddate );
		return $newdate;

Within the update function in BlockController.php – Formats the date/time string submitted from the input field generated by the Bootstrap DateTimePicker

$startDate = \DateTime::createFromFormat('m/d/Y g:i A', trim(\Input::get('date_start')));
$block->date_start = $startDate;

02 Jul

Laravel 4 and Laravel Excel to create export

A few weeks ago I jumped into Laravel and it’s been challenging. I’ve been working with PHP and JavaScript over the past countless years and before that ColdFusion and ASP. What has made the Laravel transition difficult for me is that most of the issues and resolutions posted expose the code but fail to explain where in the application it should go.

I know, I’ve read all about MVC structure and I’m starting to get it – but hopefully this might help others and if anyone has feedback or best practices – please reply!

For this project I have essentially users who have registered for an event and I call them attendees. I link to this
by dynamically pointing to /attendees/export?block_id=1. I have a table which contains regtypes – or types of registrants, for example: general and wait-list. The attexport function looks up those regtypes and generates a sheet of data for each as well a summary page with totals.

My route (routes.php):

Route::group(array('before' => 'auth'), function()
   Route::get('attendees/export', 'AttendeeController@export');


	public function export() 
		if(Input::get('block_id')) {
			$attendees = new Attendee;

attexport function from Attendee.php:

		public function attexport($block_id) {
			$report = Excel::create('export');
			$regtypes = Regtype::all();
		    $summary = $report->sheet('Summary', function($sheet) {
			foreach ($regtypes as $regtype) {
				$attendees = Attendee::where('block_id', '=', Input::get('block_id'))
					->where('regtype_id', '=', $regtype->id)
				if($attendees->count() > 0)
					$report->sheet($regtype->name, function($sheet) use ($regtype,$summary,$attendees) {
						$headers = $this->getColumnNames($attendees);

						// $attendees_array = array_merge((array)$headers, (array)$attendees->toArray());
						$attendees_array = $attendees->toArray();
						    'font' => array(
						        'name'      =>  'Arial',
						        'size'      =>  12
						    $regtype->name, $attendees->count()
			// $summary->setCellValue($celltotal,$calc);
			$summary->setCellValue('A5','Total Registered Attendees');