2017-04-08 31 views
0

我已經寫了一段代碼,但我猜測它不工作。

給定的代碼被寫入控制器:

public function store(Request $request) 
{ 
    $this -> validate($request, 
         [ 'name' => 'required' , 
         'status' => 'required', 
         'created_at' => 'required', 
         'created_by' => 'required', 
         'modified_at' => 'required', 
         'updated_by' => 'required' ]); 

    if (request('status') === 'on') 
    { 
     $status = 1; 
     # code... 
    } 
    else 
    { 
     $status = 0; 
    } 

    CategoryDetail::Create([ 'name' => request('name'), 
           'status' => $status, 
           'created_at' => request('created_at'), 
           'created_by' => request('created_by'), 
           'updated_at' => request('modified_at'), 
           'updated_by' => request('modified_by') ]); 
    \Session::flash('create', 
        'inserted successfully'); 

    return redirect('category/list'); 
} 

有沒有異常錯誤。我只想知道如何在字段留空或空時顯示消息。

這是我create.blade.php

@extends('adminlte::page') 
@section('content') 
<!DOCTYPE html> 

<html> 
    <head> 
     <title>Create</title> 
    </head> 

    <body> 
     <div class = "box box-info"> 
      <form class = "box-body form-group" 
        method = "POST" action="/category/store"> 
       Name:<input type = "text" 
          class = "form-control" 
          name = "name"><br> 
       Status:<input type = "checkbox" 
           name = "status"><br> 
       Created at:<input type = "date" 
            name = "created_at"><br> 
       Created by:<input type = "number" 
            class = "form-control" 
            name = "created_by"><br> 
       Modified at:<input type = "date" 
            name = "modified_at"><br> 
       Modified by:<input type = "number" 
            class = "form-control" 
            name = "modified_by"><br> 
       <input type = "submit" 
         value = "create" 
         name = "Create"><br> 
        {{csrf_field()}} 
        {{method_field('PUT')}} 
      </form> 
     </div> 
    </body> 
</html> 
@endsection 
+0

如果沒有例外則驗證成功嗎?通常情況下,如果驗證失敗,那麼會出現一個重定向響應,並在會話中出現錯誤,所以如果您在默認重定向位置中顯示錯誤,您應該看到它們 – apokryfos

+0

但我想看到這些消息,因爲我不知道我的代碼是否工作或不。 –

+0

在你的'/ category/list'視圖中,你需要有一個呈現消息的地方。檢查https://laravel.com/docs/5.4/validation#quick-displaying-the-validation-errors以瞭解如何呈現錯誤。你還需要檢查'session :: has('create')'是否也呈現該消息。 – apokryfos

回答

1

你可以得到錯誤按摩鑑於此:

@if (count($errors) > 0) 
         <div class="alert alert-danger alert-dismissable mb30 rtl yekan"> 
          <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button> 
          <ul class="list"> 
           @foreach($errors->all() as $err) 
            <li class="liste-item">{{ $err }}</li> 
           @endforeach 
          </ul> 
         </div> 
@endif 
+0

在哪裏插入sys_error在我的控制器 –

+0

它不工作@saman –

+0

@saman嘿謝謝,它的工作。 –

相關問題