編輯文章頁面和新建文章頁面很相似,顯示錯誤提示的代碼是相同的。下面使用局部視圖去掉兩個視圖中的重復代碼。
新建 app/views/notifications.blade.php 文件,寫入以下代碼:
~~~
@if ($errors->any())
<div id="error_explanation">
<h2>{{ count($errors->all()) }} prohibited
this article from being saved:</h2>
<ul>
@foreach ($errors->all() as $message)
<li>{{ $message }}</li>
@endforeach
</ul>
</div>
@endif
~~~
下面來修改 app/views/articles/creat.blade.php 和 edit.blade.php 視圖,使用新建的局部視圖,把其中的上面代碼全刪掉,替換成:
~~~
@include('notifications')
~~~