In this video we are going to learn about Create Services
So let see how can Create Services
First of all lets create the model and migration for Services
Go to the command prompt
And for creating the model just write here the command
Now switch to the project and lets open the migration
Inside this migration lets add columns
Now lets open the Service Model
And here lets add the table name
and create function for getting the category
Now lets open the ServiceCategory Model and and create a function for getting the services
Alright now lets migrate the migration
So switch to the command prompt and
And for migration just type the command
Now lets check the database so go to the phpMyAdmin and open the homeservicesdb databse
Now here you can see the service table
Now lets insert some records into the services table For that lets create the factory
For creating the factory type the command
Now swtich to the project and lets open this factory
So from database directory
Factories and and from here lets open this
Now write here Now open the databaseSeeder .php file
And here
Now run this seeder so go to command prompt and
Runt the command
Ok seeding complete now lets check the table
So go to the phpMyAdmin
And refresh the table and here you can see all 20 services
So in this way you can create Services
So let see how can Create Services
First of all lets create the model and migration for Services
Go to the command prompt
And for creating the model just write here the command
php artisan make:model Service –m
Now switch to the project and lets open the migration
Inside this migration lets add 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 lets open the Service Model
And here lets add the table name
and create 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');
}
}
Now lets open the ServiceCategory Model and 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 lets migrate the migration
So switch to the command prompt and
And for migration just type the command
Php artisan migrate
Now lets check the database so go to the phpMyAdmin and open the homeservicesdb databse
Now here you can see the service table
Now lets insert some records into the services table For that lets create the factory
For creating the factory type the command
php artisan make:factory ServiceFactory
Now swtich to the project and lets open this factory
So from database directory
Factories and and from here lets open this
<?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 here Now open the databaseSeeder .php file
And here
public function run()
{
\\App\\Models\\Service::factory(20)->create();
}
Now run this seeder so go to command prompt and
Runt the command
Php artisan db:seed
Ok seeding complete now lets check the table
So go to the phpMyAdmin
And refresh the table and here you can see all 20 services
So in this way you can create Services