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
index.phpinstead ofindex.blade.php. and make sure that controller returns tha value. ie,return $categoriesbeforereturn view .../from your view path and also can you post your whole viewhome.blade.php?home.blade.php?