title: Laravel 8 Undefined varible posts
body: <p>i have an error.. it says:</p>
  <p>Undefined variable: posts (View: C:\xampp\htdocs\dinsospermasdes\bansus\resources\views\admin\posts\index.blade.php)</p>
  <p>it works in home.blade.php like this:</p>
  <pre><code>@foreach ($posts as $post)
      &lt;div class=&quot;card mb-4&quot;&gt;
          &lt;img class=&quot;card-img-top&quot; src=&quot;{‌{filter_var($post-&gt;post_image, FILTER_VALIDATE_URL) ? $post-&gt;post_image : '/storage/' . $post-&gt;post_image}}&quot; alt=&quot;Card image cap&quot;&gt;
          &lt;div class=&quot;card-body&quot;&gt;
          &lt;h2 class=&quot;card-title&quot;&gt;{‌{$post-&gt;title}}&lt;/h2&gt;
          &lt;p class=&quot;card-text&quot;&gt;{‌{Str::limit($post-&gt;body, '50', '....')}}&lt;/p&gt;
          &lt;a href=&quot;{‌{route('post', $post-&gt;id)}}&quot; class=&quot;btn btn-primary&quot;&gt;Read More &amp;rarr;&lt;/a&gt;
          &lt;/div&gt;
          &lt;div class=&quot;card-footer text-muted&quot;&gt;
          Posted on {‌{$post-&gt;created_at-&gt;diffForHumans()}} by
          &lt;a href=&quot;#&quot;&gt;{‌{$post-&gt;name}}&lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      @endforeach
  </code></pre>
  <p>but didn't work in admin/posts/index.blade.php like this:</p>
  <p> </p>
  <pre><code>@foreach($posts as $post)
                        &lt;tr&gt;
                            &lt;td&gt;{‌{$post-&gt;id}}&lt;/td&gt;
                            &lt;td&gt;{‌{$post-&gt;title}}&lt;/td&gt;
                            &lt;td&gt;
                              &lt;img height=&quot;40px&quot; src=&quot;{‌{filter_var($post-&gt;post_image, FILTER_VALIDATE_URL) ? $post-&gt;post_image : '/storage/' . $post-&gt;post_image}}&quot;&gt;
                            &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;/tr&gt;
                        @endforeach
  </code></pre>
  <p>this is PostController.php:</p>
  <pre><code>class PostController extends Controller
  {
      //
  
      public function index(){
          $post = Post::all();
          return view('admin.posts.index');
      }
  
      public function show(Post $post){
          return view ('blog-post', ['post'=&gt;$post]);
      }
  
      public function create(){
          return view ('admin.posts.create');
      }
  
      public function store(){
          $inputs = request()-&gt;validate([
              'title'=&gt;'required|min:8|max:255',
              'post_image'=&gt;'file', //mime: jpeg, png
              'body'=&gt;'required'
          ]);
          if(request('post_image')){
              $inputs['post_image'] = request('post_image')-&gt;store('images');
          }
          auth()-&gt;user()-&gt;posts()-&gt;create($inputs);
          return back();
      }
  }
  </code></pre>
  <p>this is routes from web.php</p>
  <pre><code>Auth::routes();
  Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])-&gt;name('home');
  Route::get('/post/{post}', [App\Http\Controllers\PostController::class, 'show'])-&gt;name('post');    
  
  Route::middleware('auth')-&gt;group(function(){
      Route::get('/admin', [App\Http\Controllers\AdminController::class, 'index'])-&gt;name('admin.index');
  
      Route::get('/admin/posts', [App\Http\Controllers\PostController::class, 'index'])-&gt;name('post.index');
      Route::get('/admin/posts/create', [App\Http\Controllers\PostController::class, 'create'])-&gt;name('post.create');
      Route::post('/admin/posts', [App\Http\Controllers\PostController::class, 'store'])-&gt;name('post.store');
  });
  </code></pre>