frenchbaguette
frenchbaguette

Reputation: 1339

Undefined variable: $ Laravel 5

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

Upvotes: 4

Views: 69296

Answers (4)

marco hama
marco hama

Reputation: 1

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

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

Upvotes: -1

anthony zorilla
anthony zorilla

Reputation: 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    

Upvotes: -1

Ben Swinburne
Ben Swinburne

Reputation: 26467

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'));
}

Upvotes: 13

Adnan Zafar
Adnan Zafar

Reputation: 108

try to use

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

instead of

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

Upvotes: 4

Related Questions