Hi,
in my Controller i want to create an collection from Models, and return it with an Ressource Class.
When i do this (But this are not enough data in my case), i get an Json Result
$calendars = $restaurant
->calendars()
->whereBetween('date', [
Carbon::parse($request->from)->toDateString(),
Carbon::parse($request->to)->toDateString()
])
->get();
return CalendarResource::collection($calendars);
When i do this:
$googleEvents = Event::get(
Carbon::parse($request->from),
Carbon::parse($request->to)->addDay(1));
$calendars = collect();
foreach ($googleEvents as $googleEvent)
{
$googleId = explode('_', $googleEvent->id);
$calendar = Calendar::where('google_id', $googleId[0])->first();
$calendars->push($calendar);
}
return CalendarResource::collection($calendars);
I got an error:
{
"error": {
"errors": {
"file": "/home/vagrant/projects/vendor/laravel/framework/src/Illuminate/Http/Resources/DelegatesToResource.php",
"line": 120,
"exception": "ErrorException"
},
"code": 0,
"message": "Trying to get property 'id' of non-object"
}
}
What is the different between the collections?
Cheers Ralf
dd($googleEvent); inside your foreach to see if it has ->id
or
Event::whereBetween( Carbon::parse($request->from), Carbon::parse($request->to)->addDay(1))->get();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community