我想在我的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');
}
public function roles() {
return $this->belongsToMany('TicketSystem\Role');
}
public function hasAnyRoles($roles) {
return null !== $this->roles()->whereIn('name', $roles)->first();
}
public function hasAnyRole($role) {
return null !== $this->roles()->where('name', $role)->first();
}
}
在我想要添加注释的blade.php文件中:
<div>
@comments(['model' => $post])
</div>
如何解决这个问题? 我想了很多办法,但不是很成功。
编辑:comments.php文件:
return [
/**
* To extend the base Comment model one just needs to create a new
* CustomComment model extending the Comment model shipped with the
* package and change this configuration option to their extended model.
*/
'model' => \Laravelista\Comments\Comment::class,
/**
* You can customize the behaviour of these permissions by
* creating your own and pointing to it here.
*/
'permissions' => [
'create-comment' => 'Laravelista\Comments\CommentPolicy@create',
'delete-comment' => 'Laravelista\Comments\CommentPolicy@delete',
'edit-comment' => 'Laravelista\Comments\CommentPolicy@update',
'reply-to-comment' => 'Laravelista\Comments\CommentPolicy@reply',
],
/**
* The Comment Controller.
* Change this to your own implementation of the CommentController.
* You can use the \Laravelista\Comments\CommentControllerInterface.
*/
'controller' => '\Laravelista\Comments\CommentController',
/**
* Disable/enable the package routes.
* If you want to completely take over the way this package handles
* routes and controller logic, set this to false and provide your
* own routes and controller for comments.
*/
'routes' => true,
/**
* By default comments posted are marked as approved. If you want
* to change this, set this option to true. Then, all comments
* will need to be approved by setting the `approved` column to
* `true` for each comment.
*
* To see only approved comments use this code in your view:
*
* @comments([
* 'model' => $book,
* 'approved' => true
* ])
*
*/
'approval_required' => false,
/**
* Set this option to `true` to enable guest commenting.
*
* Visitors will be asked to provide their name and email
* address in order to post a comment.
*/
'guest_commenting' => false,
/**
* Set this option to `true` to enable soft deleting of comments.
*
* Comments will be soft deleted using laravels "softDeletes" trait.
*/
'soft_deletes' => false
];
在PostsController文件中:
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$post = Post::find($id);
return view('posts.show')->with('post', $post);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$post = Post::find($id);
if (auth()->user()->id !== $post->user_id) {
return redirect('/posts')->with('error', 'Unauthorized Page');
}
return view('posts.edit')->with('post', $post);
}
dd($POST,$ID)显示:
#table: "posts"
+primaryKey: "id"
+timestamps: true
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
"11"
edit.blade.php中的{{dd($POST)}}
#table: "posts"
+primaryKey: "id"
+timestamps: true
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
我假设您的配置已缓存。 重新缓存您的配置,它应该可以工作。
php artisan config:cache
此包有自己的配置文件,并使用MergeConfigFrom()
方法。 此方法在Laravel6.x中被更改为在缓存配置时不合并配置。
因此,如果在安装此包时已缓存了配置,则在安装包后重新缓存配置之前,将永远不会加载包配置。
由于没有读取包配置,配置('comments.model')
配置值将为空,您将得到所看到的错误。
一个一般性的注意事项:当您的包有问题时,检查该包的Github问题。 有人可能已经碰到了,也许有解决的办法。 例如:https://github.com/laravelista/comments/issues/103。 现在,这个“解决方案”太过分了,因为他们并不确切地知道问题是什么,但是它对您是有效的(调用PHP artisan Optimize
重新缓存配置)。