当我在laravel项目上工作时,遇到了这个错误,即使经过这么多的更改和努力,我也无法解决。我希望我能找到解决办法。
我的错误:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError引发 消息“未找到“应用/帖子”类”
CommentsController.php
<?php
namespace App\Http\Controllers;
use \Auth;
use App\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Http\Requests;
use App\Comment;
use Session;
use DB;
class CommentsController extends Controller
{
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'comment' => 'required'
]);
$post = Post::find('id');
$comments = new Comment();
$comments->name = $request->name;
$comments->email = $request->email;
$comments->comment = $request->comment;
$comments->approved = true;
$comments->post()->associate($post);
$comments->save();
Session::flash('success', 'Comment was added');
return redirect()->route('posts.show', [$post->id]);
//return redirect('/posts')->with('success', 'Comment Created
Successfully');
}
}`
Post.php `
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//Table Name
protected $table = 'posts';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamps = true;
public function user(){
return $this->belongsTo('App\User');
}
public function comment(){
return $this->hasMany('App\Comment');
}
}
`
我的Web.php(路由文件) `
Route::get('/', 'PagesController@index');
Route::get('/about', 'PagesController@about');
Route::get('/contact', 'PagesController@contact');
Route::get('/services', 'PagesController@services');
// Post Pages
Route::resource('posts', 'PostsController');
// Login Authorization
Auth::routes();
// Dashboard
Route::get('/dashboard', 'DashboardController@index');
// Comments
Route::post('/posts/{post_id}', ['uses'=>'CommentsController@store' , 'as' => 'comments.store']);
`
答案 0 :(得分:0)
注意文件Post.php
使用App \ Post;
仅A是资本,并非所有APP
答案 1 :(得分:0)
将此代码写在控制器顶部。...
use App\Post;
或
更改此代码...
$post = Post::find('id');
收件人
$post = \App\Post::find('id');
我认为这已经解决了。