I am following this tutorial: https://learn.microsoft.com/en-us/graph/tutorials/php?tutorial-step=4
However, I did not manage to list all the events in my calendar. I was able to dump json file by using this:
return response()->json($events);
However, after replacing that line with:
$viewData['events'] = $events;
return view('calendar', $viewData);
I received this error:
ErrorException Undefined variable: dateRange (View: C:\Users\syahm\FWIMS\resources\views\calendar.blade.php) http://localhost:8000/calendar Hide solutions $dateRange is undefined
My controller.blade.php:
@extends('layout')
@section('content')
<h1>Calendar</h1>
<h2>{{ $dateRange }}</h2>
<a class="btn btn-light btn-sm mb-3" href={{action('CalendarController@getNewEventForm')}}>New event</a>
<table class="table">
<thead>
<tr>
<th scope="col">Organizer</th>
<th scope="col">Subject</th>
<th scope="col">Start</th>
<th scope="col">End</th>
</tr>
</thead>
<tbody>
@isset($events)
@foreach($events as $event)
<tr>
<td>{{ $event->getOrganizer()->getEmailAddress()->getName() }}</td>
<td>{{ $event->getSubject() }}</td>
<td>{{ \Carbon\Carbon::parse($event->getStart()->getDateTime())->format('n/j/y g:i A') }}</td>
<td>{{ \Carbon\Carbon::parse($event->getEnd()->getDateTime())->format('n/j/y g:i A') }}</td>
</tr>
@endforeach
@endif
</tbody>
</table>
@endsection
My CalendarController.php:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
use App\TokenStore\TokenCache;
use App\TimeZones\TimeZones;
class CalendarController extends Controller
{
public function calendar()
{
$viewData = $this->loadViewData();
$graph = $this->getGraph();
// Get user's timezone
$timezone = TimeZones::getTzFromWindows($viewData['userTimeZone']);
// Get start and end of week
$startOfWeek = new \DateTimeImmutable('monday -1 week', $timezone);
$endOfWeek = new \DateTimeImmutable('monday', $timezone);
$queryParams = array(
'startDateTime' => $startOfWeek->format(\DateTimeInterface::ISO8601),
'endDateTime' => $endOfWeek->format(\DateTimeInterface::ISO8601),
// Only request the properties used by the app
'$select' => 'subject,organizer,start,end',
// Sort them by start time
'$orderby' => 'start/dateTime',
// Limit results to 25
'$top' => 25
);
// Append query parameters to the '/me/calendarView' url
$getEventsUrl = '/me/calendarView?'.http_build_query($queryParams);
$events = $graph->createRequest('GET', $getEventsUrl)
// Add the user's timezone to the Prefer header
->addHeaders(array(
'Prefer' => 'outlook.timezone="'.$viewData['userTimeZone'].'"'
))
->setReturnType(Model\Event::class)
->execute();
$viewData['events'] = $events;
return view('calendar', $viewData);
}
private function getGraph(): Graph
{
// Get the access token from the cache
$tokenCache = new TokenCache();
$accessToken = $tokenCache->getAccessToken();
// Create a Graph client
$graph = new Graph();
$graph->setAccessToken($accessToken);
return $graph;
}
}
Added 'dd($viewData);' to my calendarcontroller.php and I received this:
array:4 [▼
"userName" => "ABC"
"userEmail" => "ABC@localhost"
"userTimeZone" => "Eastern Standard Time"
"events" => array:3 [▼
0 => Microsoft\Graph\Model\Event {#310 ▼
#_propDict: array:6 [▼
"@odata.etag" => "W/"3ixz55KajUefR0tOGmhiSQABIRK5QA==""
"id" => "AAMkAGZlZWE2N2ZlLTczM2ItNDI2Ny05NWU0LTZmOWFiNzZmZmJhZABGAAAAAAC85uAHa4F5RpnXBxFnN2zgBwDeLHPnkpqNR59HS04aaGJJAAAAAAENAADeLHPnkpqNR59HS04aaGJJAAEhezxbAAA="
"subject" => "AA 2"
"start" => array:2 [▼
"dateTime" => "2020-11-23T09:30:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"end" => array:2 [▼
"dateTime" => "2020-11-23T16:30:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"organizer" => array:1 [▼
"emailAddress" => array:2 [▼
"name" => "AA"
"address" => "AA@localhost"
]
]
]
}
1 => Microsoft\Graph\Model\Event {#296 ▼
#_propDict: array:6 [▼
"@odata.etag" => "W/"3ixz55KajUefR0tOGmhiSQABIl5/QA==""
"id" => "AAMkAGZlZWE2N2ZlLTczM2ItNDI2Ny05NWU0LTZmOWFiNzZmZmJhZABGAAAAAAC85uAHa4F5RpnXBxFnN2zgBwDeLHPnkpqNR59HS04aaGJJAAAAAAENAADeLHPnkpqNR59HS04aaGJJAAEhezxcAAA="
"subject" => "AA 3"
"start" => array:2 [▼
"dateTime" => "2020-11-25T09:30:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"end" => array:2 [▼
"dateTime" => "2020-11-25T16:30:00.0000000"
"timeZone" => "EasternStandard Time"
]
"organizer" => array:1 [▼
"emailAddress" => array:2 [▶]
]
]
}
2 => Microsoft\Graph\Model\Event {#306 ▼
#_propDict: array:6 [▼
"@odata.etag" => "W/"3ixz55KajUefR0tOGmhiSQABIwjzEw==""
"id" => "AAMkAGZlZWE2N2ZlLTczM2ItNDI2Ny05NWU0LTZmOWFiNzZmZmJhZABGAAAAAAC85uAHa4F5RpnXBxFnN2zgBwDeLHPnkpqNR59HS04aaGJJAAAAAAENAADeLHPnkpqNR59HS04aaGJJAAEjdLLVAAA="
"subject" => "AA 4/2020 "
"start" => array:2 [▼
"dateTime" => "2020-11-26T14:30:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"end" => array:2 [▼
"dateTime" => "2020-11-26T16:00:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"organizer" => array:1 [▼
"emailAddress" => array:2 [▼
"name" => "AA"
"address" => "AA@localhost"
]
]
]
}
]
]
Removed this from my calendar.blade.php and it is now working!