1

I have the following code in my routes.php file:

Route::get('org', function()
{
    $org = Org::all();
    return View::make('org.index')
        ->with('org', $org)
        ->with('category', 'Org');
});

When I visit /org in my application I receive the following error:

ErrorException
Undefined variable: category (View: /var/www/html/.../app/views/org/index.blade.php)

I have the following code in my index.blade.php file:

@if($category == 'Org')
    @foreach($org as $organization)
     ...
    @endforeach
@endif

If I remove the if statement (which references the $category variable) everything works just fine.

Why is $category not defined in the view?

EDIT:

This is rather embarrassing, but I'm going to describe the problem in case someone else finds it useful.

I have a lot of comments and duplicate code in my routes.php file as this is my first time using Laravel and I'm testing things out. It turns out I had a duplicate of the above route (though closer to the bottom of the routes.php file) which did not pass the $category variable to the view. I removed it and this solved my problem.

I was under the impression that if there were duplicate routes, the one described first would be used.

2 Answers 2

2

Try the following:

Session::get('category')

if you have more than one variables to pass to view, you can use the following approaches:

Route::get('org', function()
{
    $org = Org::all();
    $category = 'Org';
    return View::make('org.index', compact('org', 'category'));
});

or

Route::get('org', function()
{
    $data = [];

    $data['org'] = Org::all();
    $data['category'] = 'Org';
    return View::make('org.index', $data);
});
Sign up to request clarification or add additional context in comments.

Comments

0

This is rather embarrassing, but I'm going to describe the problem in case someone else finds it useful.

I have a lot of comments and duplicate code in my routes.php file as this is my first time using Laravel and I'm testing things out. It turns out I had a duplicate of the above route (though closer to the bottom of the routes.php file) which did not pass the $category variable to the view. I removed it and this solved my problem.

I was under the impression that if there were duplicate routes, the one described first would be used.

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.