php 如何禁用 Laravel eloquent Auto Increment?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45351425/
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
How to disable Laravel eloquent Auto Increment?
提问by Mohammed F. Ouda
I need an effective way to disable Laravel from auto incriminating the primary key of the table which I am going to insert data in.
我需要一种有效的方法来禁止 Laravel 自动识别我要插入数据的表的主键。
Why? I don't check if there is any duplication between the DB and the inserted data so if there was any duplication I just handles it in a try-catch block.
为什么?我不检查数据库和插入的数据之间是否有任何重复,所以如果有任何重复,我只是在 try-catch 块中处理它。
The problem is if there was any failure Laravel counts it like I have inserted a row. So IDs column is not going to be in this order [1, 2, 3, etc], but in this [1, 4, 8, 20, etc].
问题是如果有任何失败,Laravel 会像我插入一行一样计数。所以 IDs 列不会按照这个顺序 [1, 2, 3, etc],而是按照这个 [1, 4, 8, 20, etc]。
I searched a lot about this issue and I have tried to use this line after the declaration of the class:
我对这个问题进行了很多搜索,并尝试在类声明后使用这一行:
public $autoincrement = false;
Also
还
public $incrementing = false;
But they are not working.
但他们没有工作。
I just want to use the AI of my DB. Not Laravel's one.
我只想使用我的数据库的 AI。不是 Laravel 的。
采纳答案by Mohammed F. Ouda
Sorry for taking your time guys, The issue is if we tried to save any record in MYSQL whether it returned success or failure for any reason (like for duplication in my case),
抱歉耽误你们的时间,问题是如果我们试图在 MYSQL 中保存任何记录,无论它因任何原因返回成功还是失败(例如在我的情况下重复),
MYSQL counts that the auto increment number was booked and any other coming record is not going to take it because of there may be more than an insertion process on the DB in the same time and waiting to know if the auto incrementally number is booked or not will cost MYSQL its speed.
MYSQL 计算自动增量编号已被预订,并且任何其他即将到来的记录都不会占用它,因为可能在同一时间在数据库上有多个插入过程并等待知道自动增量编号是否被预订将花费 MYSQL 的速度。
So it is not a Laravel issue, its a MYSQL one.
所以这不是 Laravel 的问题,而是 MYSQL 的问题。
I hope that it may help others.
我希望它可以帮助其他人。
Thank you all...
谢谢你们...
回答by Arun pandian M
if you wish to use a non-incrementing or a non-numeric primary key
you must set the public $incrementing property
on your model to false
.
如果你想使用 anon-incrementing or a non-numeric primary key
你必须set the public $incrementing property
在你的模型上false
。
eg :
例如:
class UserVerification extends Model
{
protected $primaryKey = 'your_key_name'; // or null
public $incrementing = false;
}
in case of migration :
在迁移的情况下:
$table->integer('id')->unsigned(); // to remove primary key
$table->primary('id'); //to add primary key
refer : https://laravel.com/docs/5.3/eloquent#eloquent-model-conventions
参考:https: //laravel.com/docs/5.3/eloquent#eloquent-model-conventions
回答by MartinTawse
Try public $incrementing = false;
尝试 public $incrementing = false;
回答by Ahmed Mahmoud
This is an example for table created its name site_rules and this is the migration file which i add the following line to make id primary and auto incremented//add this line to make id auto incremented from a specified value
DB::statement("ALTER TABLE site_rules AUTO_INCREMENT = 1;");
and this is the migration file code :
这是创建其名称为 site_rules 的表的示例,这是我添加以下行以使 id 成为主要和自动递增//add this line to make id auto incremented from a specified value
DB::statement("ALTER TABLE site_rules AUTO_INCREMENT = 1;");
的迁移文件,这是迁移文件代码:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class SiteRules extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('site_rules', function (Blueprint $table){
$table->increments('id');
$table->string('name_ar');
$table->string('name_en');
$table->timestamps();
});
//add this line to make id auto increment from a specified value
DB::statement("ALTER TABLE site_rules AUTO_INCREMENT = 1;");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('site_rules');
}
}
Try it
尝试一下
回答by aaron0207
You have to declare your new primary key in your table migration file:
您必须在表迁移文件中声明新的主键:
$table->primary('new_key_column');
Of course you have to disable autoincrement in your model as you did.
当然,您必须像以前一样禁用模型中的自动增量。
回答by d3corator
There are 2 solutions to your problem. First one is, as you said, disable the increment column. To do that, just go to your migrations, and change
您的问题有 2 种解决方案。第一个是,正如你所说,禁用增量列。为此,只需转到您的迁移,然后更改
$table->increment('id
)`
$table->increment('id
)`
to
到
$table->integer('id')
$table->integer('id')
It will remove the primary key, to set the primary key, just go to your Model file and add this:
它将删除主键,要设置主键,只需转到您的模型文件并添加以下内容:
protected $primaryKey = 'column_name';
Second solution is what I prefer. Whenever inserting, updating or removing a record and even sometimes reading a record, use laravel's DB transaction. Here is an example:
第二种解决方案是我更喜欢的。每当插入、更新或删除记录甚至有时读取记录时,请使用 laravel 的 DB 事务。下面是一个例子:
DB::beginTranscation();
try {
$model = new Model;
$model->column_name = $value;
$model->save();
DB::commit()
return;
}
catch(exception $e) {
DB::rollback();
return;
}
This approach is better than remove the auto increment. But now it's upto you to choose.
这种方法比删除自动增量更好。但现在由你来选择。
回答by Naincy
You can do something like below
您可以执行以下操作
Table: author
表:作者
Columns: id, name, active
列:id、名称、活动
class Author extends Model
{
/**
* Configure the Model variables
*
*/
protected $table = 'author';
protected $primaryKey = 'id';
protected $fillable = ['name', 'active']; // eloquent Will use only these columns as you are mentioning them as fillable.
public static function saveAuthor($data) {
$author = Author::firstOrNew(['name' => $data['name']]);
$author->name = $data['name'];
$author->active = 1;
$author->save();
}
}
Here in fillable you define the columns that you want to change or update through model. Rest fields behaviour will take according to mysql definition.
在 fillable 中,您可以定义要通过模型更改或更新的列。其余字段行为将根据 mysql 定义进行。
I hope this will help you.
我希望这能帮到您。