title: How to fix " (1/1) ErrorException compact(): Undefined variable: categories in AdminPostsController.php line 23 '"
body: <p>I want to create a post but I cannot because it shows me the <strong>Undefined variable</strong> error for the <code>compact</code> function,
  can someone help me?</p>
  
  <p>Below are the samples from files:</p>
  
  <p><strong>AdminPostsController.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function index(){
      $posts = Post::paginate(5);
  
      return view('admin.posts.index', compact('posts','categories'));
  }
  </code></pre>
  
  <p><strong>admin/posts/index.blade.php</strong></p>
  
  <pre class="lang-html prettyprint-override"><code>@extends('layouts.admin')
  
  
  @section('content')
          &lt;h1&gt;Posts page&lt;/h1&gt;
  
          &lt;table class="table"&gt;
              &lt;thead&gt;
                  &lt;th&gt;ID&lt;/th&gt;
                  &lt;th&gt;Owner&lt;/th&gt;
                  &lt;th&gt;Category&lt;/th&gt;
                  &lt;th&gt;Photo ID&lt;/th&gt;
                  &lt;th&gt;Title&lt;/th&gt;
                  &lt;th&gt;Body&lt;/th&gt;
                  &lt;th&gt;Created&lt;/th&gt;
                  &lt;th&gt;Updated&lt;/th&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
              @if($posts)
                  @foreach($posts as $post)
                  &lt;tr&gt;
                      &lt;td&gt;{{$post-&gt;id}}&lt;/td&gt;
                      &lt;td&gt;{{$post-&gt;user-&gt;name}}&lt;/td&gt;
                      &lt;td&gt;{{$post-&gt;category_id ? $post-&gt;category-&gt;name : 'No category'}}&lt;/td&gt;
  
                      &lt;td&gt;&lt;img height="50" src="{{$post-&gt;photo ? $post-&gt;photo-&gt;file : 'http://placehold.it/400x400'}}"&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;a href="{{route('admin.posts.edit', $post-&gt;id)}}"&gt;{{$post-&gt;title}}&lt;/a&gt;&lt;/td&gt;
                      &lt;td&gt;{{str_limit($post-&gt;body,10)}}&lt;/td&gt;
                      &lt;td&gt;{{$post-&gt;created_at-&gt;diffForHumans()}}&lt;/td&gt;
                      &lt;td&gt;{{$post-&gt;updated_at-&gt;diffForHumans()}}&lt;/td&gt;
                      &lt;td&gt;&lt;a href="{{route('home.post', $post-&gt;id)}}"&gt;View post&lt;/a&gt; &lt;/td&gt;
                      &lt;td&gt;&lt;a href="{{route('admin.comments.show', $post-&gt;id)}}"&gt;View comments&lt;/a&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  @endforeach
              @endif
              &lt;/tbody&gt;
          &lt;/table&gt;
  
          &lt;div class="row"&gt;
              &lt;div class="col-sm-6 col-sm-offset-5"&gt;
                  {{$posts-&gt;render()}}
                  {{--pagination--}}
              &lt;/div&gt;
          &lt;/div&gt;
  
  
  @stop
  </code></pre>