0

i am receiving an error when clicking on the post link in admin area. Error message says that the problem is in the line of code where

@foreach($posts as $post)

Not sure what the problem is.

index.blade.php

@extends('layouts.app')

@section('content')


            <div class="d-flex justify-content-left">

                <a href="{{ route('posts.create')}}" class="btn btn-success float-right">Add post</a>

                </div>

                <div class="card card-default">
                    <div class="card-header">Posts</div>

                    <div class="card-body">
                        <table class="table">
                            <thead>
                                <th>Image</th>
                                <th>Title</th>
                            </thead>
                            <tbody>
                                @foreach($posts as $post)
                                    <tr>
                                        <td>
                                            image
                                        </td>
                                        <td>
                                            {{ $post->title }}
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
    
@endsection

PostController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests\Posts\CreatePostRequest;
use Illuminate\Http\Request;
use App\Models\Post;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('posts.index')->with('posts', Post::all());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('posts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreatePostRequest $request)
    {
        //upload image to storage
        $image = $request->image->store('posts');
        //create the post
        Post::create([
            'title' => $request->title,
            'description' => $request->description,
            'content' => $request->content,
            'image' => $image
        ]);
        //flash message
        session()->flash('success', 'Post created succesfully.');
        //redirect user
        return redirect(route('posts.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
Zoran Relić
  • 45
  • 1
  • 12
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Pavel Janicek Dec 14 '20 at 12:19
  • @PavelJanicek not really relevant here as the problem seems to be with the passing of a variable from a controller to a blade template – Matt Dec 14 '20 at 12:21

1 Answers1

3

You should bring the variable too, your code should be like this :

$posts = Post::all();

return view ('posts.index', compact('posts'));
Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
daffaquraisy
  • 214
  • 2
  • 12