Laravel 8 Project Home Services - Create Services

In this video, we will learn about creating services in our Laravel 8 project.

Let's see how we can create services.

First of all, let's create the model and migration for services.

Go to the command prompt and write the following command to create the model:


php artisan make:model Service –m

Now, switch to the project and let's open the migration file.

Inside this migration file, let's add the necessary columns:


public function up()
{
Schema::create('services', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->unique();
$table->string('tagline');
$table->bigInteger('service_category_id')->unsigned()->nullable();
$table->decimal('price');
$table->decimal('discount')->nullable();
$table->enum('discount_type',['fixed','percent'])->nullable();
$table->string('image')->nullable();
$table->string('thumbnail')->nullable();
$table->longText('description')->nullable();
$table->longText('inclusion')->nullable();
$table->longText('exclusion')->nullable();
$table->boolean('status')->default(true);
$table->timestamps();
$table->foreign('service_category_id')->references('id')->on('service_categories')->onDelete('cascade');
});
}

Now, let's open the Service Model file and add the table name.

We will also create a function for getting the category:


class Service extends Model
{
use HasFactory;

protected $table = "services";

public function category()
{
return $this->belongsTo(ServiceCategory::class,'service_category_id');
}
}

Next, let's open the ServiceCategory Model file and create a function for getting the services:


class ServiceCategory extends Model
{
use HasFactory;

protected $table = "service_categories";

public function services()
{
return $this->hasMany(Service::class);
}
}

Alright, now let's migrate the migration.

Switch to the command prompt and type the following command:


Php artisan migrate

Now, let's check the database. Go to phpMyAdmin and open the homeservicesdb database.

Here, you can see the services table.

Now, let's insert some records into the services table. For that, let's create a factory.

Type the following command to create the factory:


php artisan make:factory ServiceFactory

Now, switch to the project and let's open the factory file.

Go to the database directory, then Factories, and open the file:


<?php

namespace Database\Factories;

use App\Models\Service;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class ServiceFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Service::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$service_name = $this->faker->unique()->words($nb=4,$asText=true);
$slug = Str::slug($service_name,"-");
$imageName = 'service_'. $this->faker->unique()->numberBetween(1,20). '.jpg';
return [
'name' => $service_name,
'slug' => $slug,
'tagline' => $this->faker->text(20),
'service_category_id' => $this->faker->numberBetween(1,20),
'price' => $this->faker->numberBetween(100,500),
'image' => $imageName,
'thumbnail' => $imageName,
'description' => $this->faker->text(500),
'inclusion' => $this->faker->text(20) .'|' . $this->faker->text(20) .'|' .$this->faker->text(20) .'|'. $this->faker->text(20) .'|'. $this->faker->text(20),
'exclusion' => $this->faker->text(20) .'|' . $this->faker->text(20) .'|' .$this->faker->text(20) .'|'. $this->faker->text(20) .'|'. $this->faker->text(20)
];
}
}

Now, write the necessary code in the factory file.

Next, open the databaseSeeder.php file and add the following code:


public function run()
{
\App\Models\Service::factory(20)->create();
}

Now, run the seeder. Go to the command prompt and type the following command:


Php artisan db:seed

Ok, seeding is complete. Now, let's check the table.

Go to phpMyAdmin and refresh the table. Here, you can see all 20 services.

So, in this way, you can create services.