laravel 未找到“App\Post”类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39593645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class 'App\Post' not found
提问by Brijesh Kapletiya
I am working on laravel5.2 AJAX Pagination with Jquery I have defined my post class as
我正在使用 Jquery处理laravel5.2 AJAX 分页我已将我的帖子类定义为
Post.php
后.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
<?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
请帮我把这个类粘贴到哪里去吧。我知道模型类丢失的错误,但不知道如何摆脱
My code structure is in snapshot Thanks
我的代码结构在快照中 谢谢
回答by Ravi Hirani
Your Post.php
model should be
你的Post.php
模型应该是
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
}
In BlogController.php
file add below lines on top
在BlogController.php
文件中在顶部添加以下几行
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
Read this:- https://laravel.com/docs/5.2/eloquent
回答by rupesh
You can use this code namespace App\Post;
您可以使用此代码命名空间 App\Post;
or u can use
或者你可以使用
use App\Post;
回答by Ketan Trentiya
You should add below statements at top of your Post.phpfile, like this...
您应该在Post.php文件的顶部添加以下语句,如下所示...
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
Now, in BlogController.phpadd the below statement at top of the file, like...
现在,在BlogController.php 中,在文件顶部添加以下语句,例如...
<?php
use App\Post;
will work definitely in Laravel 5>=
肯定会在 Laravel 5>= 中工作
回答by Rama Durai
If you try this,
如果你试试这个
<? php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
<? php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
Use this.. Maybe it will help you.. and note that controller file in
Folder App\Http\Controllers..
使用这个.. 也许它会帮助你.. 并注意文件
夹 App\Http\Controllers中的控制器文件..
回答by Brijesh Kapletiya
In my case, I had renamed Appnamespace to CustomAppearlier. and I am using it with
就我而言,我之前已将App命名空间重命名为CustomApp。我正在使用它
use App\Post;
Instead, I have to use
相反,我必须使用
use CustomApp\Post;
回答by anwerj
Add the namespace in the beginning of file like and use Model rather than eloquent class to extend
在文件开头添加命名空间 like 并使用 Model 而不是 eloquent class 来扩展
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
}
It will work for laravel >5.0 .
它适用于 laravel >5.0 。