[英]Laravel 5: Call to undefined method Illuminate\Database\Eloquent\Collection::exists()
I am trying to implement an Authorization Gate on the comments list on posts. 我正在尝试在帖子的评论列表上实现授权门。 I think I've managed to get it right because it works. 我想我已经设法做到了,因为它有效。
If the user is the owner of the comment, or is a moderator in the subreddit the comment is posted in, then he may edit it. 如果用户是评论的所有者,或者是subreddit中的主持人,则评论会被发布,然后他可以对其进行编辑。 If not, he won't be able to see the edit link (I'm using inline editing with X-Editable) 如果没有,他将无法看到编辑链接(我正在使用X-Editable进行内联编辑)
All works well until I submit a new comment, than I get the following error. 一切顺利,直到我提交新评论,而不是我得到以下错误。 But the comment get submitted to the database because I can see it on refresh. 但是注释被提交到数据库,因为我可以在刷新时看到它。
Undefined variable: isModerator on comment_list.blade.php 未定义的变量:comment_list.blade.php上的isModerator
It's obvious that I need to pass isModerator
variable to the view, so I did 很明显,我需要将isModerator
变量传递给视图,所以我做到了
$isModerator = $post->subreddit->moderators->where('user_id', Auth::id())->exists();
This throws this error on page load, the entire post page crashes 这会在页面加载时抛出此错误,整个帖子页面崩溃
Call to undefined method Illuminate\\Database\\Eloquent\\Collection::exists() 调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: exists()
CommentController::view_data() in PostsController.php line 90 PostsController.php第90行中的CommentController :: view_data()
This is my Gate
Authorization. 这是我的Gate
授权。 Please note that update-post
and update-sub
work without a glitch. 请注意update-post
和update-sub
工作没有故障。
public function boot(GateContract $gate)
{
parent::registerPolicies($gate);
$gate->define('update-comment', function($user, $comment, $isModerator) {
if($user->id === $comment->user_id) {
return true;
}
if ($isModerator) {
return true;
}
});
$gate->define('update-post', function ($user, $post, $isModerator) {
if ($user->id === $post->subreddit->user->id) {
return true;
}
if ($user->id === $post->user_id) {
return true;
}
if ($isModerator) {
return true;
}
return false;
});
$gate->define('update-sub', function($user, $subreddit) {
if($user->id === $subreddit->user->id) {
return true;
}
return false;
});
}
The view_data()
method in CommentController
所述view_data()
在方法CommentController
public static function view_data(Request $request, Post $post) {
$instance = new Self;
$per_page = session('per_page')?session('per_page'):config('constants.per_page');
$post = Post::with('user.votes')->with('subreddit.moderators')->with('comments')->where('id', $post->id)->first();
$comment = $post->comments;
$user = User::where('id', '=', Auth::id())->get();
$isModerator = $post->subreddit->moderators->where('user_id', Auth::id())->exists();
$result['per_page'] = $per_page;
$result['comments'] = $instance->comment_list($per_page, $request, $post, $comment, $user, $isModerator);
$result['total_comments'] = $instance->total_comments($post);
return $result;
}
The edit portion of the view looks like this 视图的编辑部分如下所示
@can('update-comment', [$each_comment, $isModerator])
<p>
<a href="#" class="testedit" data-pk="{{ $each_comment->id }}" data-url="{{ url($each_comment->post_id . '/comment/update') }}">
{!! $each_comment->comment !!}
</a>
</p>
@else
<p>
{!! $each_comment->comment !!}
</p>
@endcan
And this is show()
method in PostsController
这是PostsController
show()
方法
public function show(Post $post, User $user, Request $request, Comment $comment)
{
$post = Post::with('user.votes')->with('subreddit.moderators')->findOrFail($post->id);
$ids = $post->subreddit;
$isModerator = $ids->moderators()->where('user_id', Auth::id())->exists(); // this is line 90
$modList = Moderator::where('subreddit_id', '=', $post->subreddit->id)->get();
$view_data = CommentController::view_data($request, $post, $comment, $isModerator);
return view('post/show', $view_data)->with('post', $post)
->with('modList', $modList)
->with('isModerator', $isModerator);
}
Relations 关系
Comment
Model Comment
模型
public function posts() {
return $this->belongsTo('App\Post');
}
public function user() {
return $this->belongsTo('App\User');
}
public function commentvotes() {
return $this->hasMany('App\CommentVote');
}
Post
Model Post
模型
public function user() {
return $this->belongsTo('App\User');
}
public function subreddit() {
return $this->belongsTo('App\Subreddit');
}
public function votes() {
return $this->hasMany('App\Vote');
}
public function moderators() {
return $this->hasMany('App\Moderator');
}
public function comments() {
return $this->hasMany('App\Comment');
}
Subreddit
Model Subreddit
模型
public function user() {
return $this->belongsTo('App\User');
}
public function posts() {
return $this->hasMany('App\Post');
}
public function moderators() {
return $this->hasMany('App\Moderator');
}
Moderator
Model Moderator
模型
public function subreddit() {
return $this->belongsTo('App\Subreddit');
}
public function user() {
return $this->belongsTo('App\User', 'user_id');
}
public function posts() {
return $this->belongsTo('App\Post');
}
Since I don't know the usage of ->exists()
, but the basic premise is simply checking if the current user is a Moderator
of a specific Subreddit
, you can simply do the following: 由于我不知道->exists()
的用法,但基本前提只是检查当前用户是否是特定Subreddit
的Moderator
,您可以简单地执行以下操作:
$check = $ids->moderators()->where('user_id', Auth::id())->first();
$isModerator = $check != NULL ? true : false;
// If $check doesn't return NULL (ie returns a Moderator object) then you are a moderator.
I think you $isModerator = $ids->moderators()->where('user_id', Auth::id())
return NULL instead of collection object.So you can't use ->exists()
method. 我认为你$isModerator = $ids->moderators()->where('user_id', Auth::id())
返回NULL而不是集合对象。所以你不能使用->exists()
方法。 Did you try to use whereHas \\ has methods instead of ->where('user_id', Auth::id())
? 您是否尝试使用whereHas \\ has方法而不是->where('user_id', Auth::id())
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.