Try   HackMD

只有發布者才能編輯與刪除他發布的文章

好用的Blade語法實例02

可以先看這篇 好用的Blade語法實例01(點我)

文章只有發布者才能修改與刪除。
只有發布者才能修改與刪除他的文章。
發布者與他發布的文章、商品、各種東西的關係。

需要先滿足條件:
文章的資料表與使用者的資料表關聯。
每個發布者可以有很多篇文章,每篇文章只有一個發布者。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

@guest @else @if (Auth::user()->id == $item->user_id) <div class="card-footer text-muted"> <a href="/product/edit/{{$item->id}}" class="btn btn-warning"><i class="fas fa-edit"></i></a> <a href="/product/delete/{{$item->id}}" class="btn btn-danger"><i class="fas fa-trash-alt"></i></a> </div> @endif @endguest

上面的程式碼,是先透過一組@guest @endguest包裹外層,用意是先判斷瀏覽者是否是觀光客(未登入),如果是觀光客則看不到裡面的內容。
中間插入@else,從@else@endguest這一段,如果你不是觀光客,則可以看到內容,然後進行下一步的驗證。
這段裡面再插入@if@endif,其中@if後面加上(Auth::user()->id == $item->user_id),意思是說,當你在登入狀態時,Auth驗證從user資料表取得的你的id,如果這個id等同於這篇文章發布者的id,則你可以看到包起來的程式碼,也就是編輯跟刪除的按鈕。

tags: laravel laravel.7 blade