3

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

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?

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
  • how about $student->registraion_number = $student->department_id + year + xxx ; + = Concatenate , and use helper function for current year or Carbon and your xxx value. Commented Apr 25, 2016 at 6:54

1 Answer 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;
Sign up to request clarification or add additional context in comments.

3 Comments

It should be department code $student->department_code But that also will be came when a user will select a department, And there is no student table where I can get that '$currentId ` Value , I think there could be counter where I can keep track of that XXX ??
Well, in this case you really should show your full code. Also, you're telling me there is no student table, but in your code you're trying to save data somewhere. And you'll definitely will save registration number somewhere, so you could just use that table's ID.
Threre is a problem with $currentId, because second time I store some data it's showing following Error, Object of class App\Student could not be converted to int

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.