Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Laravel Basics MVC (Model) Deploying Migrations

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Undefined @foreach variable

Hi all,

I have a Laravel generated undefined variable when trying to write my photos view. I have my records ready but I don't know to fix the area. I'm guessing I need to write the variable somewhere and it's passed to the view in some way but I just don't know where.

What am I missing?

I've tried reseeding and remigrating the database. HELP! :)

MainController.php
<?php

namespace App\Http\Controllers;
use App\Photo;

use Illuminate\Http\Request;

$photos = Photo::all();

class MainController extends Controller
{
    //function list all photos
    public function index() {
        $photos = Photo::all();
        return view('photos', ["photos" => $photos] );
    }
}
photos.blade.php
@extends('main')

@section('content')

    <h2>Photos List</h2>

    @foreach( $photos as $photo )

        <article class="photo-container" id="photo_one">    

            <article class="photo-container" id="photo_two">

                <h3>{{ $photo->photo_title }}</h3> (<span id="photo_id">{{ $photo->photo_number }}</span> )

                <img src= "{{ $photo->photo_url }}" alt="alt" title="title" />

            </article> 

        </article>

    @endforeach

@endsection
PhotosTableSeeder.php
<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class PhotosTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //create data
        DB::table('photos')->insert([
            'photo_number' => '1',
            'photo_title' => 'Boo #1',
            'photo_url' => 'img/boo_v1.png'
        ]);

        DB::table('photos')->insert([
            'photo_number' => '2',
            'photo_title' => 'Boo #2',
            'photo_url' => 'img/boo_v2.png'
        ]);

        //create data
        DB::table('photos')->insert([
            'photo_number' => '3',
            'photo_title' => 'Boo #3',
            'photo_url' => 'img/boo_v3.png'
        ]);
    }
}
web.php
Route::get('/', 'MainController@index');


Route::get('/', function () {
    return view('home');
    //return view('main');
});

Route::get('/photos', function () {
    return view('photos');
    //return view('main');
});


Route::get('/about', function () {
    return view('about');
    //return view('main');
});

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Jonathan Grieve this looks correct to me. The only reason I can think of as to why this wouldn't work is if this line:

$photos = Photo::all();

is returning null/empty list. At that point, when it got over to the template, it wouldn't have anything to iterate through. I'd be really curious to know what happens if you log out the value of $photos right before you send it away to the view :smiley:

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I might just have a go at that for you. If it helps, I've separated the project into its own repository ready for when I (try to) deploy it.

https://github.com/jg-digital-media/laravel_basic

But the reason I did that was desperation. It's not the way it works in the course. I tried everything I could think of to get Laravel to recognise the variable, but I don't see that it;s needed anyway as it's a working variable needed only for the loop in Blade Templating. I too had issues with empty queries which were solved by remigrating and reseeding.

I love Laravel but it;s frustrating at times. ;)

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Jennifer Nordell

I got it working with many many hours of research and documentation checking and some tinkering with the web.php file.

I imported the photo class to the web route file and added compact() to the relevant route.

<?php

use Illuminate\Support\Facades\Route;
use App\Photo;

//Snip

Route::get('/photos', function () {

    $photos = Photo::all();
    return view('photos', compact('photos') );
    //return view('main');
});



//Snip

Is this a removed part of the course? I don't remember any photos view?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Jack,

Many apologies for confusing you with this thread. I should have made clear I adapted my code to make my own example when following along the course. Feel free to stick with Books and Authors for this course. :)