나는 기사가 있고 편집, 삭제 버튼을 두었지만 다른 사용자를 만들면 기사를 삭제할 수 있습니다.
이것은 내 코드입니다.
@auth
<div class="card-body">
<div class="align-items-center">
<a href="{{route('articles.edit', $article)}}" class="btn btn-warning">{{__("Edit")}}</a>
<form method="post" action="{{route('articles.destroy', $article)}}" style="display: inline-block">
@method('DELETE')
@csrf
<button onclick="return confirm('{{__("Are you sure you want to delete this article ?")}}')" class="btn btn-danger">{{__("Delete")}}</button>
</form>
</div>
@endauth
기사 사용자 ID를 사용하는 것이 좋습니다. 이를 추적하지 않는 경우 기사 모델에 필드를 추가 user_id
하면 기사 작성자가 표시됩니다. 그런 다음 작성자 만 자신의 기사를 삭제하거나 편집 할 수 있도록 if-check를 쉽게 만들 수 있습니다.
블레이드 페이지에서 다음을 수행하십시오.
@if($article->user_id === \Auth::user()->id)
<div class="align-items-center">
<a href="{{route('articles.edit', $article)}}" class="btn btn-warning">{{__("Edit")}}</a>
<form method="post" action="{{route('articles.destroy', $article)}}" style="display: inline-block">
@method('DELETE')
@csrf
<button onclick="return confirm('{{__("Are you sure you want to delete this article ?")}}')" class="btn btn-danger">{{__("Delete")}}</button>
</form>
</div>
@endif
컨트롤러의 destroy()
방법 에 유사한 확인을 추가하십시오 .
if($article->user_id === \Auth::user()->id)
// Do the delete
else
// return no-auth
$article
( store()
컨트롤러 의 방법으로) 저장할 때 현재 사용자를에 추가하는 것을 잊지 마십시오 .
$article->user_id = \Auth::user()->id
$article->save();
HTH
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다