use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint table) {
table->id();
table−>string(′nametable->string('username');
table->string('phone')->nullable();
table->timestamps();
});
public function down()
{
Schema::dropIfExists('users');
Schema::dropIfExists('addresses');
Schema::dropIfExists('companies');
}
}
make seeders and factories to fillup these table and make models with relationship
Sure! Here's an example of how you can create seeders and factories for the users, addresses, and companies tables and define relationships between them using Laravel's Eloquent ORM.
First, create a User model using the artisan command:
go
php artisan make:model User
Then, open the User.php model file and define the relationships:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFactory;
public function address()
{
return $this->hasOne(Address::class);
}
public function company()
{
return $this->hasOne(Company::class);
}
}
Similarly, create Address and Company models using the artisan command:
go
php artisan make:model Address
php artisan make:model Company
Then, open the Address.php model file and define the relationship with the User model:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Address extends Model
{
use HasFactory;
public function user()
{
return $this->belongsTo(User::class);
}
}
And open the Company.php model file and define the relationship with the User model:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
use HasFactory;
public function user()
{
return $this->belongsTo(User::class);
}
}
Next, create a UserFactory, AddressFactory, and CompanyFactory using the artisan command:
Open the database/factories/UserFactory.php file and define the factory:
php
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class UserFactory extends Factory
{
protected $model = User::class;
public function definition()
{
return [
'name' => $this->faker->name(),
'username' => $this->faker->userName(),
'email' => $this->faker->unique()->safeEmail(),
'phone' => $this->faker->phoneNumber(),
'website' => $this->faker->url(),
];
}
}
Similarly, open the database/factories/AddressFactory.php file and define the factory:
php
<?php
namespace Database\Factories;
use App\Models\Address;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class AddressFactory extends Factory
{
protected $model = Address::class;
public function definition()
{
return [
'user_id' => User::factory(),
'street' => $this->faker->streetAddress(),
'suite' => $this->faker->secondaryAddress(),
'city' => $this->faker->city(),
'zipcode' => $this->faker->postcode(),
'lat' => $this->faker->latitude(),
'lng' => $this->faker->longitude(),
];
}
}
And open the database/factories/CompanyFactory.php file and define the factory:
php
<?php
namespace Database\Factories;
use App\Models\Company;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class CompanyFactory extends Factory
{
protected $model = Company::class;
public function definition()
{
return [
'
>
faker−>
company(),′catchPhrase′=>
php
Now that we have the models, factories, and relationships set up, we can use the seeders to populate the database with sample data. Here's an example of how you can create a DatabaseSeeder to seed the users, addresses, and companies tables:
```php
<?php
namespace Database\Seeders;
use App\Models\Address;
use App\Models\Company;
use App\Models\User;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
User::factory(10)
->has(Address::factory())
->has(Company::factory())
->create();
}
}
```The `run` method of the DatabaseSeeder creates 10 users, each with an associated address and company, using the factories we defined earlier.
To run the seeder, use the `db:seed` Artisan command:
php artisan db:seed
code
And that's it! You now have sample data in the `users`, `addresses`, and `companies` tables, with relationships between them defined in the models.