Revize 92577cfd
Přidáno uživatelem Marek Lovčí před téměř 5 roky(ů)
app/Artefact.php | ||
---|---|---|
7 | 7 |
class Artefact extends Model |
8 | 8 |
{ |
9 | 9 |
// Table Name |
10 |
protected $table = 'artefacts';//unnecessery?
|
|
10 |
protected $table = 'artefacts'; |
|
11 | 11 |
// Primary Key |
12 |
public $primaryKey = 'id';//unnecessery? |
|
13 |
|
|
14 |
/** |
|
15 |
* The model's default values for attributes. |
|
16 |
* |
|
17 |
* @var array |
|
18 |
*/ |
|
19 |
/*protected $attributes = [ |
|
20 |
'likes' => 0, |
|
21 |
];*/ |
|
12 |
public $primaryKey = 'id'; |
|
22 | 13 |
|
23 | 14 |
/** |
24 | 15 |
* Indicates if the model should be timestamped. |
... | ... | |
50 | 41 |
{ |
51 | 42 |
return $this->belongsToMany('App\User'); |
52 | 43 |
} |
53 |
|
|
54 |
|
|
55 |
|
|
56 | 44 |
} |
app/Category.php | ||
---|---|---|
9 | 9 |
// Table Name |
10 | 10 |
protected $table = 'categories'; |
11 | 11 |
// Primary Key |
12 |
public $primaryKey = 'id';//unnecessery? |
|
13 |
|
|
12 |
public $primaryKey = 'id'; |
|
14 | 13 |
|
15 | 14 |
/** |
16 | 15 |
* Indicates if the model should be timestamped. |
... | ... | |
20 | 19 |
public $timestamps = false; |
21 | 20 |
|
22 | 21 |
/** |
23 |
* Get the artefacts for the catagory.
|
|
22 |
* Get the artefacts for the category.
|
|
24 | 23 |
*/ |
25 | 24 |
public function artefacts() |
26 | 25 |
{ |
27 | 26 |
return $this->belongsToMany('App\Artefact'); |
28 | 27 |
} |
29 |
|
|
30 |
|
|
31 | 28 |
} |
app/Metadata.php | ||
---|---|---|
7 | 7 |
class Metadata extends Model |
8 | 8 |
{ |
9 | 9 |
// Table Name |
10 |
protected $table = 'metadata';//same name could make problems
|
|
10 |
protected $table = 'metadata'; |
|
11 | 11 |
// Primary Key |
12 |
public $primaryKey = 'id';//unnecessery? |
|
13 |
|
|
14 |
/** |
|
15 |
* The model's default values for attributes. |
|
16 |
* |
|
17 |
* @var array |
|
18 |
*/ |
|
19 |
/*protected $attributes = [ |
|
20 |
'likes' => 0, |
|
21 |
];*/ |
|
12 |
public $primaryKey = 'id'; |
|
22 | 13 |
|
23 | 14 |
/** |
24 | 15 |
* Indicates if the model should be timestamped. |
app/User.php | ||
---|---|---|
2 | 2 |
|
3 | 3 |
namespace App; |
4 | 4 |
|
5 |
use Illuminate\Contracts\Auth\MustVerifyEmail; |
|
6 | 5 |
use Illuminate\Foundation\Auth\User as Authenticatable; |
7 | 6 |
use Illuminate\Notifications\Notifiable; |
8 | 7 |
|
... | ... | |
13 | 12 |
// Table Name |
14 | 13 |
protected $table = 'users'; |
15 | 14 |
// Primary Key |
16 |
public $primaryKey = 'id';//unnecessery?
|
|
15 |
public $primaryKey = 'id'; |
|
17 | 16 |
|
18 | 17 |
/** |
19 | 18 |
* The attributes that are mass assignable. |
database/factories/ArtefactFactory.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
/** @var Factory $factory */ |
|
4 |
/** @var Faker $faker */ |
|
5 |
|
|
6 |
use App\Artefact; |
|
7 |
use App\Category; |
|
8 |
use Faker\Generator as Faker; |
|
9 |
use Illuminate\Database\Eloquent\Factory; |
|
10 |
|
|
11 |
$factory->define(Artefact::class, function ($faker) { |
|
12 |
return [ |
|
13 |
'name' => $faker->opera, |
|
14 |
'author' => $faker->name, |
|
15 |
'made_in' => $faker->countryCode, |
|
16 |
'publisher' => $faker->company, |
|
17 |
'year' => rand(1800, 2020), |
|
18 |
'pages' => rand(0, 2000), |
|
19 |
'main_category_id' => factory(Category::class)->create()->id, |
|
20 |
]; |
|
21 |
}); |
database/factories/CategoryFactory.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
/** @var Factory $factory */ |
|
4 |
|
|
5 |
use App\Category; |
|
6 |
use Faker\Generator as Faker; |
|
7 |
use Illuminate\Database\Eloquent\Factory; |
|
8 |
|
|
9 |
$factory->define(Category::class, function (Faker $faker) { |
|
10 |
return [ |
|
11 |
'nameCZ' => $faker->colorName, |
|
12 |
'nameEN' => $faker->hexColor, |
|
13 |
]; |
|
14 |
}); |
database/factories/MetadataFactory.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
/** @var Factory $factory */ |
|
4 |
/** @var Faker $faker */ |
|
5 |
|
|
6 |
use App\Artefact; |
|
7 |
use App\Metadata; |
|
8 |
use Faker\Generator as Faker; |
|
9 |
use Illuminate\Database\Eloquent\Factory; |
|
10 |
|
|
11 |
$factory->define(Metadata::class, function ($faker) use ($factory) { |
|
12 |
return [ |
|
13 |
'name' => $faker->streetName, |
|
14 |
'noteCZ' => $faker->paragraph, |
|
15 |
'noteEN' => $faker->paragraph, |
|
16 |
'page' => rand(0, 2000), |
|
17 |
'artefact_id' => factory(Artefact::class)->create()->id |
|
18 |
]; |
|
19 |
}); |
database/factories/UserFactory.php | ||
---|---|---|
1 | 1 |
<?php |
2 | 2 |
|
3 |
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
|
3 |
/** @var Factory $factory */ |
|
4 | 4 |
|
5 | 5 |
use App\User; |
6 | 6 |
use Faker\Generator as Faker; |
7 |
use Illuminate\Database\Eloquent\Factory; |
|
7 | 8 |
use Illuminate\Support\Str; |
8 | 9 |
|
9 | 10 |
/* |
... | ... | |
22 | 23 |
'name' => $faker->name, |
23 | 24 |
'email' => $faker->unique()->safeEmail, |
24 | 25 |
'email_verified_at' => now(), |
25 |
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
|
|
26 |
'password' => bcrypt($faker->unique()->password),
|
|
26 | 27 |
'remember_token' => Str::random(10), |
28 |
'created_at' => now(), |
|
29 |
'updated_at' => now(), |
|
27 | 30 |
]; |
28 | 31 |
}); |
database/seeds/DatabaseSeeder.php | ||
---|---|---|
1 | 1 |
<?php |
2 | 2 |
|
3 | 3 |
use Illuminate\Database\Seeder; |
4 |
use Illuminate\Support\Facades\DB; |
|
5 |
use Illuminate\Support\Str; |
|
4 | 6 |
|
5 | 7 |
class DatabaseSeeder extends Seeder |
6 | 8 |
{ |
... | ... | |
11 | 13 |
*/ |
12 | 14 |
public function run() |
13 | 15 |
{ |
14 |
// $this->call(UsersTableSeeder::class); |
|
16 |
DB::table('users')->insert([ |
|
17 |
'name' => 'admin', |
|
18 |
'email' => 'admin@kaplicky.com', |
|
19 |
'email_verified_at' => now(), |
|
20 |
'password' => bcrypt('admin'), |
|
21 |
'remember_token' => Str::random(10), |
|
22 |
'created_at' => now(), |
|
23 |
'updated_at' => now(), |
|
24 |
]); |
|
25 |
|
|
26 |
// Populate users |
|
27 |
factory(App\User::class, 19)->create(); |
|
28 |
|
|
29 |
// Populate categories |
|
30 |
factory(App\Category::class, 80)->create(); |
|
31 |
|
|
32 |
// Populate artefacts with metadata |
|
33 |
factory(App\Artefact::class, 300)->create()->each(function ($u) { |
|
34 |
$u->metadata()->saveMany(factory(App\Metadata::class, 10)->make()); |
|
35 |
}); |
|
36 |
|
|
37 |
// Get all the categories attaching up to 1 random categories to each user |
|
38 |
$categories = App\Category::all(); |
|
39 |
|
|
40 |
// Populate the artefact_category pivot table |
|
41 |
App\Artefact::all()->each(function ($artefact) use ($categories) { |
|
42 |
$artefact->categories()->attach( |
|
43 |
$categories->random(1)->pluck('id')->toArray() |
|
44 |
); |
|
45 |
}); |
|
46 |
|
|
47 |
// Get all the metadata attaching up to 50 random metadata to each user |
|
48 |
$artefacts = App\Metadata::all(); |
|
49 |
|
|
50 |
// Populate the artefact_user pivot table |
|
51 |
App\User::all()->each(function ($user) use ($artefacts) { |
|
52 |
$user->likesArtefacts()->attach( |
|
53 |
$artefacts->random(rand(1, 50))->pluck('id')->toArray() |
|
54 |
); |
|
55 |
}); |
|
56 |
|
|
57 |
// Get all the metadata attaching up to 50 random metadata to each user |
|
58 |
$metadata = App\Metadata::all(); |
|
59 |
|
|
60 |
// Populate the metadata_user pivot table |
|
61 |
App\User::all()->each(function ($user) use ($metadata) { |
|
62 |
$user->likesMetadata()->attach( |
|
63 |
$metadata->random(rand(1, 50))->pluck('id')->toArray() |
|
64 |
); |
|
65 |
}); |
|
15 | 66 |
} |
16 | 67 |
} |
Také k dispozici: Unified diff
Seeding