Support the ongoing development of Laravel.io →
posted 6 years ago

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

Last updated 3 years ago.
0

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();

Last updated 6 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ralf ralf Joined 26 Jan 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.