4

This is the code from CategoriesController:

 public function index()
{
    $categories = Category::all();
    return view('/home', compact('categories'));
}

And this is the code that I'm writing to the home.blade.php

                @foreach($categories as $cat)
                @endforeach

And then in home.blade.php

I'm getting this error: Undefined variable: categories

Why this happening ? Everything works fine with Articles but not categories and the code is the same.

Home.blade.php

@extends('app')

@section('content')

     <div class="col-md-4 pull-right">
      <div class="panel panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title">Categories</h3>
        </div>
        <div class="panel-body">

                          @foreach($categories as $cat)
                          <div class="col-lg-6">
                            <ul class="list-unstyled">
                                <li><a href="articles/category/{{  }}">News</a>
                                </li>
                            </ul>
                        </div>                          
                          @endforeach
   </div>
      </div>     
    </div>


  @if(count($articles))

    @foreach($articles as $article)

<div class="panel panel-default col-md-8">
  <div class="panel-heading">

  <h3>{{ $article->title }}</h3>
   <small>posted by <a href="users/{{ $article->author->name }}">{{ $article->author->name }}</a> {{ $article->created_at }}</small>
  </div>
  <div class="panel-body">
  {!! str_limit($article->body, 1000) !!} 
    <hr>
  <a href="{{ url('/articles/'.$article->slug) }}">Read More</a>   
  </div>
</div>

        @endforeach
    @else
        <h1>No articles at the moment.</h1>
    @endif
    <br>
    {!! $articles->render() !!}
@stop
7
  • Please verify no other view is there like index.php instead of index.blade.php. and make sure that controller returns tha value. ie, return $categories before return view ... Commented Jul 7, 2015 at 9:16
  • Try removing / from your view path and also can you post your whole view home.blade.php? Commented Jul 7, 2015 at 9:26
  • Removing "/" doesnt work. I added home.blade.php @IvankaTodorova Commented Jul 7, 2015 at 9:34
  • It looks OK to me. Are you sure that the variable error comes from that exact view home.blade.php? Commented Jul 7, 2015 at 9:51
  • I dont think so. If I make view let's say to "contact" page then the same error occurs, I dont get what could be the problem Commented Jul 7, 2015 at 9:53

4 Answers 4

13

You have two routes

Route::get('/home', 'ArticlesController@index');
Route::get('/home', 'CategoriesController@index');

Which means when you visit /home, only ArticlesController@index is fired, not both.

This means that the $categories variable used in the view is not populated because your index() method in CategoriesController is intended to do this but is never called.

Therefore you need to do something like this

Route::get('/home', 'HomeController@index');

public function index()
{
    $categories = Category::all();
    $articles   = Article::all();

    return view('/home', compact('articles', 'categories'));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah! Thank you sir! Everything is working like a charm!
4

try to use

return View::make('/home')->with(compact('categories'));

instead of

return view('/home', compact('categories'));

1 Comment

Either syntax is valid, and always has been.
-1

I insert this in the top of my @foreach

 <?php
     use App\models\PersonalContact;
     $data = PersonalContact::all();
 ?>

or this before @foreach add @if statement like this

 @if (count($data) > 0)
            @foreach ($data as $bloginfo)
                <tr role="row" class="odd">
                    <td class="sorting_1">{{$bloginfo->name}}</td>
                    <td>{{$bloginfo->description}}</td>
                    <td>{{$bloginfo->email}}</td>
                    <td>{{$bloginfo->phone}}</td>
                    <td><img src="{{url('images/'.$bloginfo->image)}}" alt="" width="100px" height="100px"></td>
                <td><a href="{{url('admin/{admin}/personalcontact/create')}}" class="btn btn-app"><i class="fas fa-edit"></i>Add</a> | <a href="{{url('admin/{admin}/personalcontact/{personalcontact}/edit')}}" class="btn btn-app"><i class="fas fa-edit"></i>Edit</a> | <a class="btn btn-app"><i class="fas fa-edit"></i>Delete</a></td>
                </tr>
            @endforeach
        @endif    

1 Comment

Please add some explanation to your answer by editing it, such that others can learn from it
-1
return view('/home', compact('categories'));

The problem was $categories on compact method, you must use like a string

1 Comment

It looks like that is already what they have in the example they posted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.