Recently I’ve found out a nice little way to speed up generating of database stuff – I used to use make:migration and make:model Artisan commands separately. Apparently, they can be combined into one.
So if you run a command like this:
artisan make:model Books -m
It will create a migration file automatically for you. The magic is in -m parameter.
The whole thing looks like this:
artisan make:model Books -m Model created successfully. Created Migration: 2015_10_19_012129_create_books_table
And the result is that we have two files generated:
app/Books.php:
namespace App; use Illuminate\Database\Eloquent\Model; class Books extends Model { // }
And database/migrations/2015_10_19_012129_create_books_table.php:
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateBooksTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('books', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('books'); } }
Notice that migration file comes with automatically suggested auto_increment field and timestamps. Isn’t that sweet of Laravel? Constantly saving our time, second by second.
Awesome. Thanks for the handy tip 🙂
If you run this command it will create Model, Controller and migration.
“artisan make:model Books -m -c”
Should’t the model name be on singular? “Book” and the migration (table) in plural “books” ?
Yes, agree, this article was written long time ago. It should be Book.
Is there a way to generate a migration file after a current model? I have a User model with Customer and Roles relationships, it would be nice to have the migrations for this generated for me somehow..