Creating a car rental admin using Laravel 9 part 1: Setup database, model, migration, seeder.

Database

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=rental_mobil
DB_USERNAME=root
DB_PASSWORD=

Instalation

composer create-project laravel/laravel rental-mobil
cd rental-mobil
php artisan serve

Migration

Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('image')->nullable();
$table->boolean('is_admin')->default(0);
$table->rememberToken();
$table->timestamps();
});
php artisan make:migration create_cars_table
Schema::create('cars', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('plat');
$table->text('description');
$table->boolean('status')->default(1);
$table->double('price');
$table->timestamps();
});
php artisan make:migration create_user_details_table
Schema::create('user_details', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('nik')->nullable();
$table->string('ktp')->nullable(); //image
$table->text('alamat')->nullable();
$table->timestamps();
});
php artisan make:migration create_transactions_table
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreignId('car_id')->references('id')->on('cars')->onDelete('cascade');
$table->date('date_start');
$table->date('date_end');
$table->date('date_due');
$table->string('status');
$table->double('total');
$table->text('note');
$table->timestamps();
});

Model

php artisan make:model Car
php artisan make:model UserDetail
php artisan make:model Transaction
public function detail(){
return $this->hasOne(UserDetail::class);
}
public function transactions(){
return $this->hasMany(Transaction::class);
}
protected $guarded = [];
public function user(){
return $this->belongsTo(User::class);
}
protected $guarded = [];
public function user(){
return $this->belongsTo(User::class);
}
public function car(){
return $this->belongsTo(User::class);
}
protected $guarded = [];
public function transactions(){
return $this->hasMany(Transaction::class);
}

Seeder

php artisan make:seeder UserSeeder
$user = User::create([
'name' => 'admin',
'email' => 'admin@admin.com',
'password' => Hash::make('12345678'),
'is_admin' => 1,
]);
UserDetail::create([
'user_id' => $user->id,
]);
$this->call([
UserSeeder::class
]);
php artisan migrate --seed

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
M Alfa Riza

M Alfa Riza

FullStack Developer | Vue js | React js | Laravel