A.lakkantha
A.lakkantha

Reputation: 119

laravel-Auto increment Id

How to get auto increment Id?I created the database.It has customer registration form.Customer Id is auto-generated in DB.I want to fill Id input field by a max value of Id column.ex:example table is DB table.text field is interface.

my source code(register.blade.php)

@extends('layerout.registration')
@section('content')

<div class="container">
  <div class="row">
  <div class="col-md-4 col-md-offset-4">
    <form action="/insert" method="post">

      <center><font color="white"><h2>Registration form</h2></font></center>

      <div class="form-group">

        {{csrf_field() }}
        <script type="text/javascript">



            <label for="example-number-input" class="col-2 col-form-label">ID</label>


            <input class="form-control" type="number" value="1" name="id">


 </script>
  <label    for="example-text-input" class="col-2 col-form-label">Name</label>
   <input class="form-control" type="text" value="name" name="Name">


  <label for="example-text-input" class="col-2 col-form-label">Job_titleID</label>
    <select   class="form-control" type="text" value="0"  name="Job_ID"> <option>-0-</option>

        @foreach($register as $cat)
     <option value="{{$cat->Job_ID}}" name="Job_ID">{{$cat->Job_ID}}</option>
@endforeach

    </select>






   <label for="example-text-input" class="col-2 col-form-label">Identifier</label>

    <input class="form-control" type="text" value="93xxxxxxV" name="Identifier">

  <label for="example-text-input" class="col-2 col-form-label">Street</label>


    <input class="form-control" type="text" value="Panagoda road" name="Road">

  <label for="example-text-input" class="col-2 col-form-label">city</label>

    <input class="form-control" type="text" value="Rakwana" name="City">

     <label for="example-text-input" class="col-2 col-form-label">Postal Code</label>

<input class="form-control" type="text" value="7000" name="P_code">

  <label for="example-text-input" class="col-2 col-form-label">Countery</label>

    <input class="form-control" type="text" value="Sri Lanaka" name="Address">

  <label for="example-email-input" class="col-2 col-form-label">Email</label>

    <input class="form-control" type="email" value="[email protected]" name="email">

  <label for="example-tel-input" class="col-2 col-form-label">Telephone</label>

    <input class="form-control" type="tel" value="+94 071 172 6818" name="tel">


  <label for="example-datetime-local-input" class="col-2 col-form-label">Date and time</label>

    <input class="form-control" type="datetime-local" value="2011-08-19T13:45:00" name="datetime-local">
    <br>
    <br>

    <button type="Submit" name="Submit" value="Add" id="add" class="btn btn-primary">Submit</button>


</div>

</form>
</div>
</div>
</div>
@endsection

my controller

<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


use App\Job_ID;


class register extends Controller{






public function register()
{



    $register=Job_ID::all();
    return view('cutomer.register',['register' => $register]);

}



function insert(Request $req)
{

$ID=$req->input('ID');
$Name=$req->input('Name');
$Job_ID=$req->input('Job_ID');
$Identifier=$req->input('Identifier');
$Road=$req->input('Road');
$City=$req->input('City');
$P_code=$req->input('P_code');
$Address=$req->input('Address');
$email=$req->input('email');
$tel=$req->input('tel');
$datetimelocal=$req->input('datetime-local');

$data = array('E_ID'=>$ID,"E_Name"=>$Name,"Job_ID"=>$Job_ID,"E_Phone"=>$Identifier,"Road" =>$Road,"City" =>$City,"P_Code" =>$P_code,"E_Address" =>$Address,"E_email"=>$email,"E_Phone" =>$tel,"Date"=>$datetimelocal);

DB::table('employer')->insert($data);

}
}

I created ID column and set auto increment it in Mysql.I want to display max value(4) on Id text-field of customer registration form.

Upvotes: 3

Views: 23393

Answers (3)

alibenski
alibenski

Reputation: 11

If you are asking how to show the id of a Model, first in your controller, make a collection query like:

$posts= Post::all();
return view(view.post)->withPosts($posts);

and then on your blade file, make a foreach to iterate through all the posts:

@foreach ($postsas $post)
<p>This is ID number {{ $post->id }}</p>
@endforeach

If you are asking how to manually auto increment a certain column, this is the code I used to manually make an auto-increment column (form_counter) starting with value of 1. Basically, I check if there is an existing record, if there is none, the first record for the form_counter column is 1. If it is existing, add 1 to the most current value in the column.

$lastValueCollection = Post::where('formId', $request->inputFormId)->orderBy('Term', 'desc')->first();
        $form_counter = 1;
        if(isset($lastValueCollection->form_counter)){
            $form_counter = $lastValueCollection->form_counter + 1;    
        }
    $data = array('E_ID'=>$ID,....., 'Client_ID'=>$form_counter);

DB::table('employer')->insert($data);

This is not exact but I hope this gives you an idea.

Upvotes: 1

Yohanes Christian
Yohanes Christian

Reputation: 99

with mysql you can do this

SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "databaseName"
AND TABLE_NAME = "tableName"

for laravel

$table = DB::select("SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'databaseName' AND TABLE_NAME = 'tableName'");
if (!empty($table)) { echo $table[0]->AUTO_INCREMENT; }

Upvotes: 4

asrulsibaoel
asrulsibaoel

Reputation: 506

on your schema builder: $table->increments('yourPrimaryKey');

example :

Schema::table('users', function (Blueprint $table) {
    $table->increments('id');
});

See the the docs: https://laravel.com/docs/5.5/migrations

Upvotes: 0

Related Questions