在 Laravel 中更新和删除
Update and Delete in Laravel
我希望用户只能对自己的帖子(在本例中为评论)执行删除和更新等操作。
我有两个问题需要解决:
来自不同用户的多条评论显示在同一页面上,我在每条评论旁边都有更新或删除的链接。如果评论是他们创建的,我只希望链接显示在评论旁边。
我已经有了创建、更新和删除的功能(尽管更新和删除目前不考虑用户是谁)。我不知道我是否需要实施 Gates、Policies 或两者,以及这将如何影响我已经创建的内容……例如,我是否必须将控制器函数中的代码移至 Policy?我查看了 Laravel 有帮助的资源,但我仍然不清楚。
类 已定义关系,例如:
class User extends Authenticatable
{
public function comments()
{
return $this->hasMany(Comment::class);
}
我有在我的控制器中执行操作的功能,例如:
public function addComment(Request $request, $id)
{
$request->validate([
'body' => 'required',
]);
$entry = new Comment();
$film = Film::find($id);
$entry->body = $request->body;
$entry->film_id = $film->id;
$entry->user_id = auth()->user()->id;
$entry->save();
return redirect('/');
}
public function updateComment(Request $request)
{
$request->validate([
'body' => 'required',
]);
$entry = Comment::find($id);
$entry->body = $request->body;
$entry->save();
}
评论table:
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('film_id');
$table->string('body');
$table->timestamps();
评论blade:
<h1>{{ $film->title }}</h1>
<!--<p>{{$film->comments}}</p>-->
<table id="myTable">
<tr>
<th>Comment</th>
<th>User</th>
@if (Auth::check())
<th>Update</th>
<th>Delete</th>
@endif
@foreach ($film->comments as $comment)
<tr>
<td>{{$comment->body}}</td>
<td>{{$comment->user['name']}}</td>
@if (Auth::check())
<td><a href="/update/{{$comment->id}}">Update</a></td>
<td><a href="/delete/{{$comment->id}}">Delete</a></td>
@endif
</tr>
@endforeach
</table>
@if (Auth::check())
<div>@include('form')</div>
@else
<h3>Please log in to add a comment</h3>
@endif
@endsection
路线示例:
Route::get('/update/{id}', 'FilmsController@editComment')->name('editComment')->middleware('auth');
我目前只是在未登录的情况下使用 @if (Auth::check())
隐藏链接,但这不是针对我的问题的 suitable 解决方案。
您可以将登录用户的用户id与当前评论的用户id进行比较,如optional(Auth::user())->id === $comment->user_id
。
注意这里的optional()
。这需要注意 Auth::user()
可以 return null
如果没有用户登录。没有 Laravel 会抛出错误,因为我们在 [=14 上调用 ->id
=].
下面我将这个片段添加到您的代码中:
@foreach ($film->comments as $comment)
<tr>
<td>{{$comment->body}}</td>
<td>{{$comment->user['name']}}</td>
@if(optional(Auth::user())->id === $comment->user['id'])
<td><a href="/update/{{$comment->id}}">Update</a></td>
<td><a href="/delete/{{$comment->id}}">Delete</a></td>
@endif
</tr>
@endforeach
如果您使用 "registered policy",您将可以使用 "can" 和 "cant"。 can/cant 在 blade 和用户上工作。
$用户->可以('create', Post::class)
$user->can('update', $post_instance)
@can('update', $post_instance)
...等等...
https://laravel.com/docs/5.5/authorization#registering-policies
我希望用户只能对自己的帖子(在本例中为评论)执行删除和更新等操作。
我有两个问题需要解决:
来自不同用户的多条评论显示在同一页面上,我在每条评论旁边都有更新或删除的链接。如果评论是他们创建的,我只希望链接显示在评论旁边。
我已经有了创建、更新和删除的功能(尽管更新和删除目前不考虑用户是谁)。我不知道我是否需要实施 Gates、Policies 或两者,以及这将如何影响我已经创建的内容……例如,我是否必须将控制器函数中的代码移至 Policy?我查看了 Laravel 有帮助的资源,但我仍然不清楚。
类 已定义关系,例如:
class User extends Authenticatable
{
public function comments()
{
return $this->hasMany(Comment::class);
}
我有在我的控制器中执行操作的功能,例如:
public function addComment(Request $request, $id)
{
$request->validate([
'body' => 'required',
]);
$entry = new Comment();
$film = Film::find($id);
$entry->body = $request->body;
$entry->film_id = $film->id;
$entry->user_id = auth()->user()->id;
$entry->save();
return redirect('/');
}
public function updateComment(Request $request)
{
$request->validate([
'body' => 'required',
]);
$entry = Comment::find($id);
$entry->body = $request->body;
$entry->save();
}
评论table:
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('film_id');
$table->string('body');
$table->timestamps();
评论blade:
<h1>{{ $film->title }}</h1>
<!--<p>{{$film->comments}}</p>-->
<table id="myTable">
<tr>
<th>Comment</th>
<th>User</th>
@if (Auth::check())
<th>Update</th>
<th>Delete</th>
@endif
@foreach ($film->comments as $comment)
<tr>
<td>{{$comment->body}}</td>
<td>{{$comment->user['name']}}</td>
@if (Auth::check())
<td><a href="/update/{{$comment->id}}">Update</a></td>
<td><a href="/delete/{{$comment->id}}">Delete</a></td>
@endif
</tr>
@endforeach
</table>
@if (Auth::check())
<div>@include('form')</div>
@else
<h3>Please log in to add a comment</h3>
@endif
@endsection
路线示例:
Route::get('/update/{id}', 'FilmsController@editComment')->name('editComment')->middleware('auth');
我目前只是在未登录的情况下使用 @if (Auth::check())
隐藏链接,但这不是针对我的问题的 suitable 解决方案。
您可以将登录用户的用户id与当前评论的用户id进行比较,如optional(Auth::user())->id === $comment->user_id
。
注意这里的optional()
。这需要注意 Auth::user()
可以 return null
如果没有用户登录。没有 Laravel 会抛出错误,因为我们在 [=14 上调用 ->id
=].
下面我将这个片段添加到您的代码中:
@foreach ($film->comments as $comment)
<tr>
<td>{{$comment->body}}</td>
<td>{{$comment->user['name']}}</td>
@if(optional(Auth::user())->id === $comment->user['id'])
<td><a href="/update/{{$comment->id}}">Update</a></td>
<td><a href="/delete/{{$comment->id}}">Delete</a></td>
@endif
</tr>
@endforeach
如果您使用 "registered policy",您将可以使用 "can" 和 "cant"。 can/cant 在 blade 和用户上工作。
$用户->可以('create', Post::class)
$user->can('update', $post_instance)
@can('update', $post_instance) ...等等...
https://laravel.com/docs/5.5/authorization#registering-policies