DEV Community

Alfa Riza
Alfa Riza

Posted on

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

Hello everyone, in this article we will create a car rental application using Laravel and bootstrap 5.

Database

for the database architecture I’ve made it on dbdiagram

DbDiagram

we have a users table, user_details to store user data, cars table to store car data and transactions table to store transaction data. In the .env file, don’t forget to set the database

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

Instalation

the next step we will install laravel 9, here i use composer for installation. Run the following command:

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

then, the initial appearance of our application as follows:

Image description

Migration

Our next step will be setting migration according to the database architecture that we created earlier. In the migration create table user change it to like this

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();
});

Next we create a migration for the cars table by running the command

php artisan make:migration create_cars_table

Next on the migration cars file, change it to

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();
});

Next we create a user details table that will be directly related to the users table. Run the following command to create a user_details migration.

php artisan make:migration create_user_details_table

In user_details migration, change to

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();
});

Next we create a migration for table transactions, run the following command:

php artisan make:migration create_transactions_table

Pada table transactions ubah menjadi berikut :

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

Every migration that we have made, we will also create a model so that we can use Eloquent ORM. The model we will create is User, UserDetail, Car, Transaction. Create a model by running the following command:

php artisan make:model Car
php artisan make:model UserDetail
php artisan make:model Transaction

In User model add fillable image and is_admin, and function

public function detail(){
return $this->hasOne(UserDetail::class);
}
public function transactions(){
return $this->hasMany(Transaction::class);
}

In the UserDetail model, add the following code:

protected $guarded = [];
public function user(){
return $this->belongsTo(User::class);
}

in the Transaction model add code like this:

protected $guarded = [];
public function user(){
return $this->belongsTo(User::class);
}
public function car(){
return $this->belongsTo(User::class);
}

on the Car model add the following code:

protected $guarded = [];
public function transactions(){
return $this->hasMany(Transaction::class);
}

Seeder

We will create 1 user for admin by creating a seeder, run the following command:

php artisan make:seeder UserSeeder

In the UserSeeder add code like this:

$user = User::create([
'name' => 'admin',
'email' => 'admin@admin.com',
'password' => Hash::make('12345678'),
'is_admin' => 1,
]);
UserDetail::create([
'user_id' => $user->id,
]);

Don’t forget to call UserSeeder on DatabaseSeeder

$this->call([
UserSeeder::class
]);

And run the migrate command

php artisan migrate --seed

In the next article, we will create auth for our application, thank you :)

Top comments (0)