Class 'App\Post' not found

All we need is an easy explanation of the problem, so here it is.

I am working on laravel 5.2 AJAX Pagination with Jquery I have defined my post class as

Post.php

<?php

class Post extends Eloquent
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'posts';

    /**
     * Define guarded columns
     *
     * @var array
     */
    protected $guarded = array('id');
}

BlogController.php

<?php

class BlogController extends Controller
{
  /**
     * Posts
     *
     * @return void
     */
    public function showPosts()
    {
      $posts = Post::paginate(5);

        if (Request::ajax()) {
            return Response::json(View::make('posts', array('posts' => $posts))->render());
        }

        return View::make('blog', array('posts' => $posts));
    }
}

Kindly help me where to paste this class to go right .
I know its error that model class is missing but dont know how to get rid of

enter image description here

My code structure is in snapshot
Thanks

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Your Post.php model should be

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //
}

In BlogController.php file add below lines on top

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Post;

Read this:- https://laravel.com/docs/5.2/eloquent

Method 2

Add the namespace in the beginning of file like and use Model rather than eloquent class to extend

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Post extends Model {

}

It will work for laravel >5.0 .

Method 3

You should add below statements at top of your Post.php file, like this…

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

Now, in BlogController.php add the below statement at top of the file, like…

<?php

use App\Post;

will work definitely in Laravel 5>=

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply