Laravel“类名必须是有效的对象或字符串”

我想在我的 Laravel 应用程序中的帖子后添加评论。我找到了我想使用的这个包https://github.com/laravelista/comments 。我安装了它,并按照说明进行操作,我现在遇到的问题是

类名必须是有效的对象或字符串(查看:C:\xampp\htdocs\lsapp\resources\views\vendor\comments\components\comments.blade.php)

它向我显示了这段代码:

 {


        return tap(new $class, function ($instance) {


            if (! $instance->getConnectionName()) {


                $instance->setConnection($this->connection);


            }


        });


    }

这段代码不是我写的,而是我安装前面提到的包后生成的。


我的 Post.php 文件在这里:



namespace TicketSystem;


use Illuminate\Database\Eloquent\Model;

use Laravelista\Comments\Commentable;


class Post extends Model

{

    use Commentable;

    protected $table = 'posts'; //default?

    public $primaryKey = 'id';

    public $timestamps = true; //default


    public function user() {

        return $this->belongsTo('TicketSystem\User');

    }

}

我的 User.php 文件在这里:



namespace TicketSystem;


use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Notifications\Notifiable;

use Illuminate\Contracts\Auth\CanResetPassword;

use Laravelista\Comments\Commenter;


class User extends Authenticatable

{

    use Notifiable, Commenter;


    /**

     * The attributes that are mass assignable.

     *

     * @var array

     */

    protected $fillable = [

        'name', 'email', 'password',

    ];


    /**

     * The attributes that should be hidden for arrays.

     *

     * @var array

     */

    protected $hidden = [

        'password', 'remember_token',

    ];


    /**

     * The attributes that should be cast to native types.

     *

     * @var array

     */

    protected $casts = [

        'email_verified_at' => 'datetime',

    ];


    public function posts() {

        return $this->hasMany('TicketSystem\Post');

    }

如何解决这个问题呢?我搜索了很多关于如何做的信息,但不是很成功。


回首忆惘然
浏览 285回答 3
3回答

Cats萌萌

我假设您的配置已缓存。重新缓存您的配置,它应该可以工作。php artisan config:cache该包有自己的配置文件,并使用该mergeConfigFrom()方法。此方法在 Laravel 6.x 中更改为在缓存配置时不合并配置。因此,如果您在安装此软件包时已经缓存了您的配置,则在安装软件包后重新缓存您的配置之前,将永远不会加载软件包配置。由于未读取包配置,因此config('comments.model')配置值将为空,并且您将收到所看到的错误。

呼如林

只需创建您的缓存php artisan config:cache composer dump-autoload

翻翻过去那场雪

对于像我这样的新手,如果您在部署时遇到错误并且想知道如何删除缓存中的配置,请执行以下操作;sudo rm (YOUR PROJECT PATH HERE)/bootstrap/cache/config.php在ubuntu服务器上
打开App,查看更多内容
随时随地看视频慕课网APP