2017-03-18 49 views
0

我使用Laravel 5.4,具有AJAX調用後方法,它調用寄存器控制器(內置)並返回JSON格式的錯誤,我想只能返還消息只有它的機身: 例子:Laravel只返回第一個消息,只有它的內容

"first_last_name":["First and last names are required"] => only "First and last names are required" to be returned. 

我是相當新的Laravel並認爲這裏的驗證:

public function register(Request $request) 
    { 
     $this->validator($request->all())->validate(); // here i tried to use ->messages and -> first, but doesn't not seem to work 

     event(new Registered($user = $this->create($request->all()))); 

// commented because i don't want auto login and redirect 
     //$this->guard()->login($user); 

     //return $this->registered($request, $user) 
      //    ?: redirect($this->redirectPath()); 
    } 

我嘗試使用 - >消息()和 - >第一(),但我似乎無法使其工作。

謝謝

+0

如果您需要幫助,您應該展示更多代碼和您想要做什麼 –

+0

謝謝,更新 –

回答

0

可以使用get方法讓每一個請求屬性。

例子:

$firstName = $request->get('first_last_name');

有了這個,你可以單獨驗證每個屬性。

+0

我並不是每個存在的財產,未來都可能發生變化,我無法阻止它,這樣我說我需要第一個來自錯誤 –