laravel 如何在laravel中为特定表获取自动生成字段的下一个ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37210747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:47:51  来源:igfitidea点击:

How to get next id of autogenerated field in laravel for specific table?

phplaravellaravel-5.2

提问by fico7489

I am looking for something like :

我正在寻找类似的东西:

DB::table('users')->getNextGeneratedId();

not

不是

$user->save($data)
$getNextGeneratedId = $user->id;

Does anybody know hot to achieve this ?

有人知道实现这一目标的热吗?

回答by Leandro Parice

This work for me: (PHP: 7.0 - Laravel 5.5)

这对我有用:(PHP:7.0 - Laravel 5.5)

use DB;

$statement = DB::select("SHOW TABLE STATUS LIKE 'users'");
$nextId = $statement[0]->Auto_increment;

回答by Sergey.R

You need to execute MySQL query to get auto generated ID.

您需要执行 MySQL 查询以获取自动生成的 ID。

show table status like 'users'

In Laravel5, you can do as following.

在 Laravel5 中,您可以执行以下操作。

public function getNextUserID() 
{

 $statement = DB::select("show table status like 'users'");

 return response()->json(['user_id' => $statement[0]->Auto_increment]);
}

回答by Naren

In Laravel5, you can do as following.

在 Laravel5 中,您可以执行以下操作。

$data = DB::select("SHOW TABLE STATUS LIKE 'users'");

$data = array_map(function ($value) {
    return (array)$value;
}, $data);

$userId = $data[0]['Auto_increment'];

回答by Anshul Lonkar

In MySQL you can get auto generated id by this query.

在 MySQL 中,您可以通过此查询获取自动生成的 ID。

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

回答by i komang sugiartana

Try this:

尝试这个:

$id = DB::table('INFORMATION_SCHEMA.TABLES')
    ->select('AUTO_INCREMENT as id')
    ->where('TABLE_SCHEMA','your database name')
    ->where('TABLE_NAME','your table')
    ->get();

回答by MingalevME

For PostgreSQL:

对于 PostgreSQL:

<?php // GetNextSequenceValue.php

namespace App\Models;

use Illuminate\Support\Facades\DB;

trait GetNextSequenceValue
{
    public static function getNextSequenceValue()
    {
        $self = new static();

        if (!$self->getIncrementing()) {
            throw new \Exception(sprintf('Model (%s) is not auto-incremented', static::class));
        }

        $sequenceName = "{$self->getTable()}_id_seq";

        return DB::selectOne("SELECT nextval('{$sequenceName}') AS val")->val;
    }
}

The model:

该模型:

<?php // User.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use GetNextSequenceValue;
}

The result:

结果:

<?php // tests/Unit/Models/UserTest.php

namespace Tests\Unit\Models;

use App\Models\User;
use Tests\TestCase;

class UserTest extends TestCase
{
    public function test()
    {
        $this->assertSame(1, User::getNextSequenceValue());
        $this->assertSame(2, User::getNextSequenceValue());
    }
}

回答by pubudu sachintha

This is the code snippet i used in laravel,witch will work perfectly

这是我在 Laravel 中使用的代码片段,witch 可以完美运行

thanks,

谢谢,

$id=DB::select("SHOW TABLE STATUS LIKE 'Your table name'");
$next_id=$id[0]->Auto_increment;
echo $next_id;