【问题标题】:Get last id inserted record to DB in Laravel [duplicate]在 Laravel 中获取最后一个 id 插入记录到 DB [重复]
【发布时间】:2020-02-06 14:15:39
【问题描述】:

我是 Laravel 的初学者。我在我的项目中使用 Laravel 5.8。

我有这个代码:

$product = new Product();
$product->create([
    'company_id' => $request->user()->company_id,
    'enable' => $request->input('enable') ?? 0,
    'id_delivery_vat' => $request->input('id_delivery_vat') ?? 1,
    'id_product_vat' => $request->input('id_product_vat') ?? 1,
    'id_producer' => $request->input('id_producer'),
    'promo' => $request->input('promo') ?? 0,
    'name' => $request->input('name'),
    'qr_code' => $request->input('qr_code'),
    'oe_code' => $request->input('oe_code'),
    'description' => $request->input('description'),
    'product_price' => str_replace(",", ".", $request->input('product_price')) ?? 0,
    'promo_product_price' => str_replace(",", ".", $request->input('promo_product_price')) ?? 0,
    'product_delivery_price' => str_replace(",", ".", $request->input('product_delivery_price')) ?? 0,
    'quantity' => $request->input('quantity'),
    'url_address' => Str::slug($request->input('name'), '-')
]);

如何获取最后插入 ID?

我的产品迁移:

Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
            //$table->bigInteger('category_id')->unsigned();
            //$table->foreign('category_id')->references('id')->on('product_categories');
            $table->smallInteger('id_delivery_vat')->unsigned();
            $table->foreign('id_delivery_vat')->references('id')->on('vat');
            $table->smallInteger('id_product_vat')->unsigned();
            $table->foreign('id_product_vat')->references('id')->on('vat');
            $table->bigInteger('id_producer')->unsigned();
            //$table->foreign('id_producer')->references('id')->on('product_producers');
            $table->string('name', 120)->nullable();
            $table->string('qr_code', 120)->nullable();
            $table->string('oe_code', 120)->nullable();
            $table->char('enable', 1)->default(0);
            $table->char('promo', 1)->default(0);
            $table->longText('description')->nullable();
            $table->decimal('product_price', 9, 2)->default(0);
            $table->decimal('promo_product_price', 9, 2)->default(0);
            $table->decimal('product_delivery_price', 9, 2)->default(0);
            $table->unsignedInteger('quantity')->default(0);
            $table->string('url_address', 160);
            $table->timestamps();
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';
        });

我的 ID 有很大的增量。

型号:

class Product extends Model
{
    use scopeActiveTrait;

    protected $fillable = ['company_id', 'id_delivery_vat', 'id_product_vat', 'id_producer', 'name', 'qr_code', 'oe_code', 'enable', 'promo', 'description', 'product_price', 'promo_product_price', 'product_delivery_price', 'quantity', 'url_address'];
    protected $quarded = ['id'];
    public $timestamps = true;

    public function categories()
    {
        return $this->belongsToMany(Category::class, 'product_selected_categories', 'product_id', 'category_id');
    }
}

我该如何修复它? 我尝试:$product->id, $product->nextId() - 但这不起作用:(

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    如果您希望最后一个插入 id 将其放入下一个插入产品中,那么您不必这样做,只需使用自动增量 id。

    如果您想将其用于其他用途,您可以简单地保存产品,然后获取 id:

    $product->save();
    $lastId = $product->id; // or whatever name you give to the id
    

    【讨论】:

    • 这返回我:null
    • 在数据库中我有正确的 ID
    • 我更新了我的主帖
    • 即使在$product->save(); 之后你也得到空值?
    • 是的,我有空
    【解决方案2】:

    这里$product->id是最后一个插入id

    【讨论】:

    • 当我想 dd($product->id) 我有 null
    • 插入真的发生了吗?你检查过你的错误日志或数据库来验证吗?
    猜你喜欢
    • 2011-03-09
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多