php -laravel如何从用户输入中保存自定义数据

时间:2016-04-25 06:07:09

标签: php jquery ajax laravel

我想在我的laravel项目中保存注册号数据

Registration Number format: <dept code>-<current year>-XXX. For example, CSE-2012-001,

当用户选择特定部门时,此处的代码将来自UI端。 我该怎么做。任何想法或可能的解决方案?

这是我的控制器:

 public function saveStudent(Request $request)
    {
         $this->validate($request,[                      
            'email' => 'required|unique:students', 
            'contact_no' => 'required|regex:/(01)[0-9]{9}/',     

            ]);
            $student = new Student();           
            $student->name = $request->Input(['name']);       

            $student->email = $request->Input(['email']);
            $student->contact_no = $request->Input(['contact_no']);     
            $student->address = $request->Input(['address']);
            $student->date = $request->Input(['date']);      
            $student->department_id=$request->Input(['department_id']);
            $student->registraion_number =            
            $teacher->save();            
            return redirect('teacherSavePage'); 
    } 

这是我的刀片视图:

@extends('layouts.master')
@section('title')
Student Registration
@endsection
@section('content')
@include('partials.message-block')
        <div class="container" >
            <h3> Student Registration </h3>
        {!! Form::open(array('route' => 'saveStudent','class'=>'form-horizontal','method'=>'POST'))  !!}
    {!! Form::token(); !!}
    {!!   csrf_field() ; !!} 

            <div class="form-group">
                <label>Name</label>
                <input type="text" name="name" class="form-control" required placeholder="Name">
            </div>

            <div class="form-group">
                <label>Email</label>
                <input type="email" name="email" class="form-control" required placeholder="Email">
            </div>

          <div class="form-group">
          <label>Phone</label>
          <input type="text" name="contact_no" class="form-control" required placeholder="Phone">
        </div>

        <div class="form-group">
          <label>Date</label>
          <input type="text" id="txtDate" name="date"  class="form-control" required >
        </div>

        <div class="form-group">
          <label>Address</label>          
          <textarea class="form-control" name="address" required placeholder="Address" rows="3"></textarea>
        </div>

        <div class="form-group">
            <label for="">Department</label>
            <select class="form-control input-sm" required name="department_id" >
            @foreach($department as $row)
            <option value="{{$row->id}}">{{$row->name}}</option>
            @endforeach
            </select>
        </div> 

            <button type="submit" class="btn btn-default">Submit</button>
        {!! Form::close() !!}
        </div>

    <script type="text/javascript">
    $(document).ready(function() {
              $('#txtDate').datepicker();
              $('#txtDate').datepicker('setDate', 'today');
      });
    </script>

@endsection

1 个答案:

答案 0 :(得分:2)

如果我理解正确,那应该是这样的:

$currentId = Student::orderBy('id', 'desc')->first()->id + 1;

$student->registraion_number = $student->department_id.'-'.date("Y").'-'.$currentId;