doufangpian5545 2018-01-30 01:54
浏览 46
已采纳

在laravel中显示重复的条目警告

how to display duplicate entry warning error to my view in laravel blade. so when they key in same name the warning will appear when they saved it.

Note: i have already make my Schema $table->string('studentname')->unique();;

Controller

public function store(Request $request)
{
    $this->validate($request, [
        'studentname'=>'required|max:50',
    ]);   
    $students = new Student();
    $students->studentname = $request->studentname;
    $students->address = $request->address;
    $students->religion = $request->religion;
    $students->save();

    return redirect()->route('students.index')
        ->with('flash_message', 'Success.');
}

View-Blade

<div class="container">
  <h1 class="well">Registration Form</h1>
  <div class="col-lg-12 well">
    <div class="row">
      <form action="{{route('students.store')}}" method="POST">
        {{csrf_field()}}
        <div class="col-sm-12">
          <h3>CHILD'S INFORMATION</h3>
          <hr>
          <div class="row">
            <div class="col-sm-4 form-group">
              <label>FULLNAME</label>
              <input type="text" name="studentname" value="" placeholder="Enter FULLNAME.." class="form-control" required>
            </div>        
            <div class="col-sm-4 form-group">
              <label>RELIGION</label>
              <input type="text" name="religion" value="" placeholder="Enter RELIGION.." class="form-control">
            </div>
            <div class="col-sm-4 form-group">
              <label>ADDRESS</label>
              <input type="text" name="address" value="" placeholder="Enter ADDRESS.." class="form-control">
            </div>
            <div>             
              <button type="submit" class="btn btn-default">SUBMIT</button>
            </div>
          </div>
        </div>
      </div>

</div>
  • 写回答

1条回答 默认 最新

  • douyihuaimao733955 2018-01-30 02:07
    关注

    Add the unique validation which returns a message if it fails.

    $this->validate($request, [
        'studentname'=>'required|max:50|unique:table_name,studentname',
    ]);
    

    And then, in your blade template, do this.

    <div class="col-sm-4 form-group {{ $errors->get('studentname') ? 'has-error' : '' }}">
      <label>FULLNAME</label>
      <input type="text" name="studentname" value="" placeholder="Enter FULLNAME.." class="form-control" required>
      @foreach($errors->get('studentname') as $error)
        <span class="help-block">{{ $error }}</span>
      @endforeach
    </div>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)