laravel-Auto increment Id

时间:2017-10-30 07:58:53

标签: javascript php mysql laravel-5

如何获得自动增量ID?我创建了数据库。它有客户注册表。客户ID在DB中自动生成。我想用Id column.ex:{{3的最大值填充Id输入字段}}

我的源代码(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="mithika.hetti@gmail.com" 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

我的控制器

<?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);

}
}

我在Mysql中创建了ID列并设置了自动增量。我想在客户注册表的Id文本字段中显示最大值(4)。

3 个答案:

答案 0 :(得分:2)

使用mysql你可以做到这一点

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

答案 1 :(得分:1)

架构构建器上的

$table->increments('yourPrimaryKey');

示例:

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

请参阅文档:https://laravel.com/docs/5.5/migrations

答案 2 :(得分:1)

如果您询问如何显示模型的ID,首先在控制器中创建一个集合查询,如:

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

然后在您的刀片文件上,制作一个foreach来遍历所有帖子:

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

如果您问如何手动自动递增某列,这是我用来手动创建自动增量列(form_counter)的代码,从值1开始。基本上,我检查是否有现有记录,如果没有,则form_counter列的第一条记录为1.如果存在,则将1添加到列中的最新值。

$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);

这不完全正确,但我希望这会给你一个想法。