(1/2) ErrorException Undefined property: Illuminate\Pagination\LengthAwarePaginator::$photos

Posted 1 year ago by almost_pitt

Hey guys,

I'm following the Build Project Flyer with Me series and am trying to modify it a bit. I have successfully been able to upload photos and their thumbnails and display them on show.blade.php. However, when I try to input the same code to display the photos on another page, such as index.blade.php, I get the following error:

(1/2) ErrorException Undefined property: Illuminate\Pagination\LengthAwarePaginator::$photos

It seems that the $photos variable was not passed to the other pages.

I have tried different ways to pass the $photos variable to the index route with no luck. Any idea what I could do here?

Here is the code that works perfectly well in the show.blade.php, but breaks index.blade.php

@foreach($rooms->photos as $photo)
  <img src="{{ '/' . $photo->thumbnail_path }}">
@endforeach 

Then, here is what I have in the RoomsController:

public function index()
{
    switch (request('filter')) {
        case 'me':
            $results = Room::where('user_id', Auth::id())->paginate(10);
        break;
        case 'least_expensive':
            $results = Room::orderBy('price', 'asc')->paginate(10);
        break;
        case 'most_expensive':
            $results = Room::orderBy('price', 'desc')->paginate(10);
        break;
        case 'soonest':
            $results = Room::orderBy('datepicker', 'asc')->paginate(10);
        break;
        case 'latest':
            $results = Room::orderBy('datepicker', 'desc')->paginate(10);
        break;
        default:
            $results = Room::orderBy('created_at', 'desc')->paginate(10);
            break;
    }
    return view('rooms.index', ['rooms' => $results]);
}

and Routes web.php file


...
Route::get('rooms/index', [
  'uses' => '[email protected]',
  'as' => 'rooms.index'
  ]);

...

Route::get('rooms/{slug}', [
  'uses' => '[email protected]',
  'as' => 'room'
  ]);

Any idea how I should pass the $photos variable within the RoomsController? Or is there something else I should be looking at?

Thanks!!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.