2018-09-02 22:07:45 +02:00
|
|
|
<?php
|
|
|
|
|
2020-11-08 20:10:42 +01:00
|
|
|
namespace Database\Seeders;
|
|
|
|
|
|
|
|
use App\Models\Link;
|
|
|
|
use App\Models\LinkList;
|
|
|
|
use App\Models\Tag;
|
|
|
|
use App\Models\User;
|
2020-11-17 19:40:25 +01:00
|
|
|
use Exception;
|
2018-09-02 22:07:45 +02:00
|
|
|
use Illuminate\Database\Seeder;
|
|
|
|
|
|
|
|
class ExampleSeeder extends Seeder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Run the database seeds.
|
|
|
|
*
|
|
|
|
* @return void
|
2020-11-17 19:40:25 +01:00
|
|
|
* @throws Exception
|
2018-09-02 22:07:45 +02:00
|
|
|
*/
|
2020-11-17 19:40:25 +01:00
|
|
|
public function run(): void
|
2018-09-02 22:07:45 +02:00
|
|
|
{
|
|
|
|
// Generate users, categories and tags
|
2020-11-08 20:10:42 +01:00
|
|
|
User::factory()->create();
|
|
|
|
$lists = LinkList::factory()->count(10)->create();
|
|
|
|
$tags = Tag::factory()->count(30)->create();
|
2018-09-02 22:07:45 +02:00
|
|
|
|
|
|
|
// Generate links and attach tags to them
|
2020-11-08 20:10:42 +01:00
|
|
|
Link::factory()->count(50)->create()->each(function (Link $link) use ($tags, $lists) {
|
2018-09-02 22:07:45 +02:00
|
|
|
if (random_int(0, 1)) {
|
|
|
|
// Attach a random number of tags to the link
|
2019-10-24 15:44:03 +02:00
|
|
|
$link->tags()->sync($tags->random(random_int(1, 8))->pluck('id'));
|
2018-09-02 22:07:45 +02:00
|
|
|
}
|
2019-10-29 16:59:42 +01:00
|
|
|
|
|
|
|
if (random_int(0, 1)) {
|
|
|
|
// Attach a random number of tags to the link
|
|
|
|
$link->lists()->sync($lists->random(random_int(1, 2))->pluck('id'));
|
|
|
|
}
|
2018-09-02 22:07:45 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|