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>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面