laravel create article

migrations

  • use increments to auto_increment id's value
    • string type can not be null when applying to id
  • use timestamp to create two fields
    • created_at
    • updated_at
  • when using factory, the table will be automately filled update time and create date to the relative fields

Create Article

1
2
3
4
// create controller with resource methods
> php artisan make:controller ArticleController --resource
// create model along with migration
> php artisan make:model Article --migration

seeder

  • there are two ways to insert data to table via command line

1
2
> php artisan db:seed
> php artisan migrate:refresh --seed

  • both ways need adding customed seeder to Databaseseeder.php
    • db:seed however only execute seeds run() function
  • if you don't want to use factory to get fake data, you can just use db:seed command to insert your customed data.
  • remember to add the seeder you create to Databaseseeder.php

set index to field(migrations)

  • belongsTo

1
2
3
 

$table->unsignedInteger('user_id')->index();

1
2
3
4
// appMessageboard.php
public function user() {
return $this->belongsTo(User::class);
}