1. Go to this page and download the library: Download ajcastro/fk-adder library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ajcastro / fk-adder example snippets
'Fk' => FkAdder\Fk::class
return [
// For simple string-based declaration
'fk_datatypes_path' => base_path('database/foreign_keys/fk_datatypes.php')
// For class-based declaration, used for special cases and more control. You don't need this for simple cases .
'fk_namespace' => 'Your\Fk\Namespace',
];
namespace Your\Fk\Namespace;
use FkAdder\BaseFk;
class UserId extends BaseFk
{
protected $referenceTable = 'users';
public function createFkColumn($column)
{
return $this->table->unsignedInteger($column);
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('group_id')->nullable()->comment('Group of the user');
$table->unsignedBigInteger('preference_id')->nullable()->comment('Preference of the user');
$table->foreign('group_id')->references('id')->on('groups');
$table->foreign('preference_id')->references('id')->on('preferences')
->onDelete('cascade')>onUpdate('cascade');
});
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->increments('id');
Fk::make($table)->add('group_id')->nullable()->comment('Group of the user');
Fk::make($table)
->onDelete('cascade')
->onUpdate('cascade')
->add('preference_id')
->nullable()
->comment('Preference of the user');
});
Fk::migrate();
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->increments('id');
// Foreign key declaration is one-liner, simpler and more compact.
// You dont have to type what datatype it is. You will just declare it once.
Fk::make($table)->add('group_id')->nullable()->comment('Group of the user');
Fk::make($table)->onDelete('cascade')->add('preference_id')
->nullable()->comment('Preference of the user');
// After you call the method `add()`, it will return an instance of the usual \Illuminate\Support\Fluent,
// so that you can chain more column declaration like `nullable()` and `comment()`
// If ever you need a different column name from the foreign key, just pass a second parameter
// to `add()` method e.g.
Fk::make($table)->add('group_id', 'new_group_id')->nullable()->comment('New group of the user');
// The default `onDelete` settings is `restrict` and `onUpdate` is `cascade`.
Fk::make($table)->onDelete('restrict')->onUpdate('cascade')->add('group_id', 'new_group_id');
// You can also pass the key name for the foreign key.
Fk::make($table)->keyName('users_new_group_id_foreign_key')->add('group_id', 'new_group_id');
// Take note that those foreign key modifiers should be called prior or before the `add()` method.
});
// Finally, you may now migrate and persist foreign keys in mysql database.
// You can call this once at the very end of migration,
// so all your foreign key declarations accross different migration files will be persisted.
Fk::migrate();
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Fk::rollback(); // delete foreign keys persisted by Fk::migrate(), (coming soon...)
Schema::dropIfExists('users');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.