/**
* Get the first message from the bag for a given key.
*
* @param string $key
* @param string $format
* @return string
*/
public function first($key = null, $format = null)
{
$messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
return count($messages) > 0 ? $messages[0] : '';
}
$errors
是laravel
在response
中自動塞進去的一個屬性,就算你代碼中沒有寫到,他還是會傳一個空的,其類型為Illuminate\Support\ViewErrorBag
但其實只是用數組乘載
Illuminate\Support\MessageBag
所以當你使用
$errors->first('email')
時,可以根據下面代碼看出來有值就回傳,沒值就回傳空,所以才不會報錯