Php Laravel::更新外键的最佳方法
我有这个迁移文件Php Laravel::更新外键的最佳方法,php,database,laravel,laravel-5.1,Php,Database,Laravel,Laravel 5.1,我有这个迁移文件 Schema::create('table_one', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->integer('table_two_id')->unsigned(); $table->foreign('table_two_id')->references('id')-
Schema::create('table_one', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->integer('table_two_id')->unsigned();
$table->foreign('table_two_id')->references('id')->on('table_two');
$table->timestamps();
});
我想进行更新,使其成为onDelete('cascade')强>
$table->foreign('table_two_id')->references('id')->on('table_two')->onDelete('cascade');
最好的方法是什么
是否有类似于->change()的东西强>
$table->foreign('table_two_id')->references('id')->on('table_two')->onDelete('cascade');
谢谢删除外键,然后再次添加并运行“迁移”
public function up()
{
Schema::table('table_one',函数(Blueprint$table){
$table->dropForeign(['table\u two\u id']);
$table->foreign('table\u two\u id')
->引用('id')
->on(‘表二’)
->onDelete(“级联”);
});
}
克里斯托弗·K.是对的,在拉拉维尔的文档中说:
要删除外键,可以使用dropForeign方法。外键约束使用与索引相同的命名约定。因此,我们将连接表名和约束中的列,然后在名称后面加上“\u foreign”:
$table->dropForeign('posts_user_id_foreign');
或者,您可以传递一个数组值,该值在删除时将自动使用常规约束名称:
$table->dropForeign(['user_id']);
如何通过控制器执行
1-设置粗糙度:路由::get('foreignkeyforimg',”foreignkey@index"); 2-使用Foreignkey名称创建控制器。
3-具有扩展自迁移类的外键控制器。
4-转到数据库并从表中手动删除旧主键
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class Foreignkey extends Migration
{
function index(){
Schema::table('images', function (Blueprint $table) {
$table->foreign('sub_cat_id')
->references('id')
->on('subcategories')
->onDelete('cascade');
});
}
}
编写器需要条令/dbal
php artisan迁移
public function up()
{
Schema::table('<tableName>', function (Blueprint $table) {
$table->dropForeign('<FK-name>');
$table->dropColumn('<FK-columnName>');
});
Schema::table('<tableName>', function (Blueprint $table) {
$table->foreignId('<FK-columnName>')->constrained()->cascadeOnDelete();
});
}
public function up()
{
Schema::table(“”,函数(Blueprint$table){
$table->dropForeign(“”);
$table->dropColumn(“”);
});
Schema::table(“”,函数(Blueprint$table){
$table->foreigned(“”)->constrained()->cascadeOnDelete();
});
}
有两个问题。在Laravel8上工作是有意义的,因为MySQL也会这样做,MySQL中没有“改变”这种情况。然而,对于这种情况,我希望在Laravel级别使用包装器/单_命令。谢谢。谢谢你的回答,不过这个命名约定既不是强制性的,也不可靠。我经常达到外键长度和重命名列的限制,这些列不重命名依赖的外键。所以我对@Borut Flis的答案很满意。你能解释一下为什么有人应该用控制器来做这件事吗?“转到数据库并删除”是什么意思?如果您为这些东西编写迁移,为什么要手动运行某些东西呢?因为通过迁移,它可能会损害其他数据库表。这是一种为任何表更新外键的简单方法。这对于新手来说非常简单,请编辑您的答案进一步解释:您所说的“伤害”是什么意思“?大多数主要的PHP脚本在迁移过程中都会运行这样的东西,我从来没有听说过任何数据库会因为更改指示而受损。您的函数不会更新,但会创建一个外键,如果外键存在,它将不起作用。问题是关于更新外键。阅读第4…4点-转到数据库并从t中手动删除旧的主键他在表中列出了错误
SQLSTATE[23000]:完整性约束冲突:1022无法写入;表中的重复键…
将在通过重新定义受约束的
来更新外键时引发,因此只需删除受约束的,并说出一些只需要更新的内容,并将其与->change()链接即可
。在我的例子中,我刚刚将一个外键更新为可空,我不需要再次重新定义constrained,因为它应该在创建迁移中调用,而不是在alter迁移中调用,除非您希望将约束更改为另一个表。