Home>

Please write your question in detail here.

Creating an Instagram copy app using laravel
If i create a post list page and execute it after creating Routing->Controller->View, the following error will occur.
I googled it and looked it up, but I couldn't find a clue to the solution and asked a question.

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class'App \ Http \ Controllers \ Post' not found
Corresponding source code
<? php
namespace App \ Http \ Controllers;
use Illuminate \ Http \ Request;
class PostsController extends Controller
{
  public function __construct ()
    {
        $this->middleware ('auth');
    }
public function index ()
  {
    $posts = Post :: limit (10)
          ->orderBy ('created_at','desc')
          ->get ();
    return view ('post/index', ['posts' =>$posts]);
  }
    public function new ()
    {
        return view ('post/new');
    }
      public function store (Request $request)
    {
        $validator = Validator :: make ($request->all (), ['caption' =>'required | max: 255','photo' =>'required']);
        if ($validator->fails ())
        {
            return redirect ()->back ()->withErrors ($validator->errors ())->withInput ();
        }
        $post = new Post;
        $post->caption = $request->caption;
        $post->user_id = Auth :: user ()->id;
        $post->save ();
        $request->photo->storeAs ('public/post_images', $post->id.'.jpg');
        return redirect ('/');
    }
}
What I tried

POST spell check (plural, singular, etc.)
Spell check of views file

Supplementary information (FW/tool version, etc.)

macOS Laravel 5.5

  • Answer # 1

    <? php
    namespace App \ Http \ Controllers;
    use Illuminate \ Http \ Request;
    // I don't know where the Post model was made, but which one?
    + use App \ Models \ Post;
    + use App \ Post;
    class PostsController extends Controller
    {
      public function __construct ()
        {
            $this->middleware ('auth');
        }
    public function index ()
      {
        $posts = Post :: limit (10)
              ->orderBy ('created_at','desc')
              ->get ();
        return view ('post/index', ['posts' =>$posts]);
      }
        public function new ()
        {
            return view ('post/new');
        }
          public function store (Request $request)
        {
            $validator = Validator :: make ($request->all (), ['caption' =>'required | max: 255','photo' =>'required']);
            if ($validator->fails ())
            {
                return redirect ()->back ()->withErrors ($validator->errors ())->withInput ();
            }
            $post = new Post;
            $post->caption = $request->caption;
            $post->user_id = Auth :: user ()->id;
            $post->save ();
            $request->photo->storeAs ('public/post_images', $post->id.'.jpg');
            return redirect ('/');
        }
    }

  • Answer # 2

    I'm looking for a PostController in routes, but maybe I don't have a PostController?
    I think this is because the questioner has named it PostsController.