首页 > 这个$errors有时候不存在,为什么blade不报错呢?

这个$errors有时候不存在,为什么blade不报错呢?


因为Laravel 5.2.27开始在每个页面都加上了web中间件,自然不会报错“$errors未定义”。
另外你直接在Views写表单代码?不用点form扩展包吗?


$errorslaravelresponse 中自動塞進去的一個屬性,就算你代碼中沒有寫到,他還是會傳一個空的,其類型為

Illuminate\Support\ViewErrorBag

但其實只是用數組乘載 Illuminate\Support\MessageBag

所以當你使用 $errors->first('email') 時,可以根據下面代碼看出來

/**
 * 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] : '';
}

有值就回傳,沒值就回傳空,所以才不會報錯

【热门文章】
【热门文章】