在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):laracasts/Laravel-5-Generators-Extended开源软件地址(OpenSource Url):https://github.com/laracasts/Laravel-5-Generators-Extended开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Extended Migration Generators for Laravel 6, 7, 8 and 9Easily define the migration schema right in your
Which allows you to do Created in 2015 by Jeffrey Way as a natural progression of his JeffreyWay/Laravel-4-Generators package, to provide the same features for Laravel 5. Since 2017 it's been maintained by the Backpack for Laravel team, with features and fixes added by community members like you. So feel free to pitch in. Table of ContentsVersionsDepending on your Laravel version, you should:
InstallationYou can install v2 of this project using composer, the service provider will be automatically loaded by Laravel itself:
You're all set. Run ExamplesMigrations With Schema
Notice the format that we use, when declaring any applicable schema: a comma-separated list...
So any of these will do:
Using the schema from earlier...
...this will give you: <?php
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->string('username');
$table->string('email')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
} When generating migrations with schema, the name of your migration (like, "create_users_table") matters. We use it to figure out what you're trying to accomplish. In this case, we began with the "create" keyword, which signals that we want to create a new table. Alternatively, we can use the "remove" or "add" keywords, and the generated boilerplate will adapt, as needed. Let's create a migration to remove a column.
Now, notice that we're using the correct Schema methods. <?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class RemoveUserIdFromPostsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function(Blueprint $table) {
$table->dropColumn('user_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function(Blueprint $table) {
$table->integer('user_id');
});
}
} Here's a few other examples of commands that you might write:
ModelsNow, when you create a migration, you typically want a model to go with it, right? By default, this package won't create a model to go with the migration. But it could. Just specify
Migration PathIf you wish to specify a different path for your migration file, you can use the
Foreign ConstraintsThere's also a secret bit of sugar for when you need to generate foreign constraints. Imagine that you have a posts table, where each post belongs to a user. Let's try:
Notice that "foreign" option (
As such, for that full command, our schema should look like so:
Neato. Pivot TablesSo you need a migration to setup a pivot table in your database? Easy. We can scaffold the whole class with a single command.
Here we pass, in any order, the names of the two tables that we need a joining/pivot table for. This will give you: <?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostTagPivotTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_tag', function(Blueprint $table) {
$table->integer('post_id')->unsigned()->index();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('post_tag');
}
}
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论