【问题标题】:Class 'App/Post' not found in laravel-5.6在 laravel-5.6 中找不到类“应用程序/帖子”
【发布时间】:2020-02-13 20:23:01
【问题描述】:

当我在我的 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']);

`

【问题讨论】:

  • Post.php 文件在哪里?
  • 文件位于app/Post.php,初始目录@Jerodev
  • 保存名为 Post.php 的文件,而不是 My post.php
  • 只要使用composer dump-autoload
  • 旁注:在您的 url '/posts/{post_id}' 中有一个参数,但您也忘记在控制器中添加它:public function store(Request $request, $post_id)$post = Post::find($post_id);

标签: php laravel laravel-5.6


【解决方案1】:

将这段代码写在控制器的顶部......

    use App\Post;

更改此代码...

    $post = Post::find('id');

    $post = \App\Post::find('id');

我认为这已经解决了。

【讨论】:

    【解决方案2】:

    注意文件Post.php

    使用应用\发布;

    just A is Capital 并非所有APP

    【讨论】:

    • 没有这样的use APP\Post;
    猜你喜欢
    • 2019-05-17
    • 2018-12-27
    • 1970-01-01
    • 2020-03-14
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多