Country Model
Below code create country model & migration files.
php artisan make:model Country -m
The fillable property specifies which attributes should be mass-assignable. Whenever we create a model, we may pass the list of attributes to the model constructor. It is possible for the user to modify any and all of the model if user input is blindly passed to the model.For this reason, all Eloquent models protect against mass-assignment by default.
We are working two fields (code, name) for Country hence we need to put in the fillable method.
Replace model class with below code.
Model class
Note: Please use the copy button to copy the source code.
class Country extends Model
{
use HasFactory;
protected $fillable = [
'code',
'name',
];
}
Migration class
Up function perform migration for creating/modifying table. Down function perform rollback of migration.
By default, id will be the primary key with an incremental integer value.
For all Available Column Types, please refer to https://laravel.com/docs/8.x/migrations
Replace the up() and down() methods with the code below.
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->id();
$table->string('code')
->index();
$table->string('name');
});
}
public function down()
{
Schema::drop('countries');
}
To create/update the table to database, we need to run artisan migrate command
php artisan migrate