第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 在字段驗證后插入記錄

Laravel 在字段驗證后插入記錄

PHP
阿波羅的戰(zhàn)車 2022-07-22 16:35:04
我正在嘗試 Laravel 5.6 請求驗證。我面臨幾個問題1.無法查看視圖中的驗證消息。2.添加驗證碼數(shù)據(jù)后不插入數(shù)據(jù)庫。(添加驗證之前它的工作)3 如何驗證下拉狀態(tài)?(應(yīng)選擇 Active/Inactive)designation.blade.php<form action="{{url('./designation/store')}}" method="POST">      <div class="form-row">        <div class="form-group col-md-7">          <label for="inputDesignation">Designation</label>          <input type="text" name="designation" class="form-control" id="inputDesignation">        </div>        <div class="form-group col-md-5">          <label for="inputStatus_Designation">Status</label>          <select name="status" id="inputStatus_Designation" class="form-control">            <option selected>Select Status</option>            <option >Active</option>            <option >Inactive</option>          </select>        </div>      </div>      <button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>      {{ csrf_field() }}  </form>指定控制器.phppublic function store(Request $request){    //Validation the Data    $validatedData = $request->validate([        'designation_type' => ['required','max:255'],        'status' => ['required'],    ],    [        'designation_type.required' => 'Designation is required',        'designation_type.max' => 'Designation should not be greater than 255 characters.',    ]);    //Data Insert into database   $data =[       'designation_type'=>$request->input('designation'),       'status'=>$request->input('status')   ];   DB::table('designation')->insert($data);   return redirect('/designation');}請幫我解決這個問題!
查看完整描述

3 回答

?
慕斯王

TA貢獻1864條經(jīng)驗 獲得超2個贊

嘗試以下代碼


public function store(Request $request)

{

    //Validation the Data


    $validatedData = $request->validate([

        'designation_type' => ['required','max:255'],

        'status' => ['required'],

    ],

    [

        'designation_type.required' => 'Designation is required',

        'designation_type.max' => 'Designation should not be greater than 255 characters.',

    ]);



    if($validatedData->fails()) {

      return Redirect::back()->withErrors($validatedData);

    }



    //Data Insert into database

   $data =[

       'designation_type'=>$request->input('designation'),

       'status'=>$request->input('status')

   ];

   DB::table('designation')->insert($data);


   return redirect('/designation');

}





<form action="{{url('./designation/store')}}" method="POST">

      <div class="form-row">

        <div class="form-group col-md-7">

          <label for="inputDesignation">Designation</label>

          <input type="text" name="designation" class="form-control" id="inputDesignation">

          @if($errors->has('designation'))

            <div class="error">{{ $errors->first('designation') }}</div>

        @endif

        </div>

        <div class="form-group col-md-5">

          <label for="inputStatus_Designation">Status</label>

          <select name="status" id="inputStatus_Designation" class="form-control">

            <option selected>Select Status</option>

            <option >Active</option>

            <option >Inactive</option>

          </select>

          @if($errors->has('status'))

            <div class="error">{{ $errors->first('status') }}</div>

        @endif

        </div>

      </div>


      <button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>

      {{ csrf_field() }}

  </form>


查看完整回答
反對 回復(fù) 2022-07-22
?
慕工程0101907

TA貢獻1887條經(jīng)驗 獲得超5個贊

     public function store(Request $request)

        {

            $this->validate($request,[

                'name' => 'required'

            ]);


            $category = new Category();

            $category->name = $request->name;

            $category->slug = str_slug($request->name);

            $category->save();

            Toastr::success('Category Successfully Saved','Success');

            return redirect()->route('admin.category.index');

        }



// blade 


<form method="POST" action="{{ route('admin.category.store') }}">

   @csrf

   <div class="form-group form-float">

     <div class="form-line">

     <input value="{{ old('name') }}" name="name" type="text" id="category_name" class="form-control">

     <label class="form-label">{{ __('Name') }}</label>

       </div>

        </div>

          <br>


         <a href="{{ route('admin.category.index') }}"  class="btn btn-danger m-t-15 waves-effect">{{ __('BACK') }}</a>

          <button type="submit" class="btn btn-primary m-t-15 waves-effect">{{ __('SUBMIT') }}</button>

</form>


查看完整回答
反對 回復(fù) 2022-07-22
?
慕森卡

TA貢獻1806條經(jīng)驗 獲得超8個贊

參考此https://laravel.com/docs/5.6/validation#named-error-bags后,我對代碼進行了一些更改,這有助于解決錯誤。


在designation.blade.php添加


 @if ($errors->any())

<div class="alert alert-danger">

    <ul>

        @foreach ($errors->all() as $error)

            <h6>{{ $error }}</h6>

        @endforeach

    </ul>

</div>

@endif

在DesignationController.php


用于狀態(tài)下拉驗證。


'status' => 'required|not_in:0',

用于數(shù)據(jù)插入數(shù)據(jù)庫部分


DB::table('designation')->insert($validatedData);

完整代碼


designation.blade.php


<div class="row">

  <div class="col-md-2"></div>

    <div class="col-md-8">

      @if ($errors->any())

        <div class="alert alert-danger">

        <ul>

        @foreach ($errors->all() as $error)

            <h6>{{ $error }}</h6>

        @endforeach

    </ul>

</div>

@endif

</div>

 <div class="col-md-2"></div>

 </div>

 <div class="row">

<div class="col-md-4"></div>

<div class="col-md-4">

  <form action="{{url('./designation/store')}}" method="POST">

      <div class="form-row">

        <div class="form-group col-md-7">

          <label for="inputDesignation">Designation</label>

          <input type="text" name="designation_type" class="form-control" id="inputDesignation">

        </div>

        <div class="form-group col-md-5">

          <label for="inputStatus_Designation">Status</label>

          <select name="status" id="inputStatus_Designation" class="form-control">

            <option selected value="">Select Status</option>

            <option value="Active">Active</option>

            <option value="Inactive">Inactive</option>

          </select>

        </div>

      </div>

      {{-- <button type="submit" class="btn btn-primary">Sign in</button> --}}

      <button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>

      {{ csrf_field() }}



  </form>

</div>

 <div class="col-md-4"></div>

</div>

指定控制器.php


public function store(Request $request)

{

    $validatedData = $request->validate([

        'designation_type' => 'required|max:255',

        'status' => 'required|not_in:0',

    ],

    [

        'designation_type.required' => 'Designation is required !!',

        'designation_type.max' => 'Designation should not be greater than 255 characters.',

        'status.required' => 'Status is required !!'

    ]);


    DB::table('designation')->insert($validatedData);

    return redirect('/designation');

}

謝謝你們花費寶貴的時間來幫助我....!!!


查看完整回答
反對 回復(fù) 2022-07-22
  • 3 回答
  • 0 關(guān)注
  • 169 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號