Skip to content

Commit eece9b8

Browse files
committed
Prepared Fictitious Data Structure (users and addresses)
- Created addresses migration; - Created addresses model; - Created relationship for users in addresses model; - Created relationship for addresses in users model; - Created factory for addresses (AddressModelFactory); - Renamed factory for users (UserModelFactory); - Created seeder for users; - Changed databaseSeeder;
1 parent c5fd2fa commit eece9b8

File tree

8 files changed

+117
-25
lines changed

8 files changed

+117
-25
lines changed

app/Address.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Address extends Model
8+
{
9+
public function user()
10+
{
11+
return $this->belongsTo(User::class);
12+
}
13+
}

app/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ class User extends Authenticatable
2626
protected $hidden = [
2727
'password', 'remember_token',
2828
];
29+
30+
public function addresses()
31+
{
32+
return $this->hasMany(Address::class);
33+
}
2934
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
$factory->define(App\Address::class, function (Faker\Generator $faker) {
5+
static $password;
6+
7+
return [
8+
'street' => $faker->streetName,
9+
'number' => $faker->randomNumber(3),
10+
'city' => $faker->city,
11+
'state' => $faker->stateAbbr,
12+
];
13+
});

database/factories/ModelFactory.php

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
$factory->define(App\User::class, function (Faker\Generator $faker) {
5+
static $password;
6+
7+
return [
8+
'name' => $faker->firstName . ' ' . $faker->lastName,
9+
'email' => $faker->unique()->safeEmail,
10+
'password' => $password ?: $password = bcrypt('secret'),
11+
'remember_token' => str_random(10),
12+
];
13+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateAddressesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('addresses', function (Blueprint $table) {
17+
$table->increments('id');
18+
19+
$table->integer('user_id')->unsigned();
20+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
21+
22+
$table->string('street');
23+
$table->integer('number');
24+
$table->string('city');
25+
$table->string('state');
26+
$table->timestamps();
27+
});
28+
}
29+
30+
/**
31+
* Reverse the migrations.
32+
*
33+
* @return void
34+
*/
35+
public function down()
36+
{
37+
Schema::dropIfExists('addresses');
38+
}
39+
}

database/seeds/DatabaseSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
1111
*/
1212
public function run()
1313
{
14-
// $this->call(UsersTableSeeder::class);
14+
$this->call(UsersTableSeeder::class);
1515
}
1616
}

database/seeds/UsersTableSeeder.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Seeder;
4+
use Illuminate\Support\Facades\Schema;
5+
6+
class UsersTableSeeder extends Seeder
7+
{
8+
/**
9+
* Run the database seeds.
10+
*
11+
* @return void
12+
*/
13+
public function run()
14+
{
15+
Schema::disableForeignKeyConstraints();
16+
17+
App\User::truncate();
18+
App\Address::truncate();
19+
20+
Schema::enableForeignKeyConstraints();
21+
22+
factory(App\User::class, 10)->create()->each(function ($user) {
23+
24+
$faker = app()->make(\Faker\Generator::class);
25+
26+
for ($i = 0; $i < $faker->numberBetween(1,5); $i++)
27+
{
28+
$address = factory(App\Address::class)->make();
29+
$user->addresses()->save($address);
30+
}
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy