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

时间:2022-10-22 20:55:57

I want to save Registration Number data in my laravel project where

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

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

Here dept code will come from UI side when a user select a particular department. How do I do it..Any idea or possible solution?

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

Here is my controller:

这是我的控制器:

 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'); 
    } 

Here is my blade View :

这是我的刀片查看:

@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 个解决方案

#1


2  

If I understood you correctly, it should be something like this:

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

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

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

#1


2  

If I understood you correctly, it should be something like this:

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

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

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