为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
回首忆惘然 2023-07-08 22:02:36
我想在我的 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');    }如何解决这个问题呢?我搜索了很多关于如何做的信息,但不是很成功。
查看完整描述

3 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

我假设您的配置已缓存。重新缓存您的配置,它应该可以工作。

php artisan config:cache

该包有自己的配置文件,并使用该mergeConfigFrom()方法。此方法在 Laravel 6.x 中更改为在缓存配置时不合并配置。

因此,如果您在安装此软件包时已经缓存了您的配置,则在安装软件包后重新缓存您的配置之前,将永远不会加载软件包配置。

由于未读取包配置,因此config('comments.model')配置值将为空,并且您将收到所看到的错误。

查看完整回答
反对 回复 2023-07-08
?
呼如林

TA贡献1798条经验 获得超3个赞

只需创建您的缓存

php artisan config:cache

composer dump-autoload


查看完整回答
反对 回复 2023-07-08
?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

对于像我这样的新手,如果您在部署时遇到错误并且想知道如何删除缓存中的配置,请执行以下操作;

sudo rm (YOUR PROJECT PATH HERE)/bootstrap/cache/config.php

在ubuntu服务器上


查看完整回答
反对 回复 2023-07-08

添加回答

代码语言

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号