0

I want to foreach some data but it says it doesn't know the variable:

The error I get:

    ErrorException in dbda158712a631f22ffd888cd244c74e60f3a433.php line 51:
Undefined variable: albums (View:        /var/www/clients/client2/web2/web/resources/views/album.blade.php)

Here is my code:
Album.blade.php

@foreach($albums as $others)
    <option value="{{$others->id}}">{{$others->name}}</option>
@endforeach

My album controller function

  public function getAlbum($id)
  {
    $album = Album::with('Photos')->find($id);
    return View::make('album')
    ->with('album',$album);
  }

  public function getList()
  {
    $albums = Album::with('Photos')->get();
    return View::make('index')
    ->with('albums',$albums);
  }
5
  • Use @foreach($album as $others) Commented Sep 25, 2016 at 19:06
  • @CharlotteDunois that gives me the following error: Trying to get property of non-object Commented Sep 25, 2016 at 19:07
  • Post the full error message Commented Sep 25, 2016 at 19:08
  • what about return view('album', ['album' => $album]): Commented Sep 25, 2016 at 19:08
  • ErrorException in dbda158712a631f22ffd888cd244c74e60f3a433.php line 52: Trying to get property of non-object (View: /var/www/clients/client2/web2/web/resources/views/album.blade.php) Commented Sep 25, 2016 at 19:08

2 Answers 2

3

You are passing album variable with view Album.blade.php, which is single object, not array of object so you can't iterate in a loop.

I think you are doing a mistake.

You want to do foreach in index.blade.php, because here you are passing the albums variable.

or

you need to return view album.blade.php in your getList function.

Sign up to request clarification or add additional context in comments.

2 Comments

But I need the getList in my index aswell, is there a way I can return it in multiple views?
You want list in both blade file? if yes then you can't return two view in one function but you can pass multiple variable with a view
0

Try This Code.

 public function getAlbum($id)
 {
      $albums=albums::with('Photos')->get();

       $album=albums::with('Photos')->find($id);

       return view('album',compact('albums','album'));

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.