46 lines
1.3 KiB
PHP
46 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Document;
|
|
use App\Models\DocumentCategory;
|
|
use App\Models\DocumentTag;
|
|
use Illuminate\Database\Seeder;
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
|
|
|
class DocumentSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run(): void
|
|
{
|
|
Document::factory(3)->make()->each(function (Document $model) {
|
|
$model->path = FileHelper::storeFile(
|
|
$model,
|
|
new \SplFileInfo(\resource_path(
|
|
'../database/factories/assets/news/525x295.jpg'
|
|
))
|
|
);
|
|
$model->saveOrFail();
|
|
});
|
|
|
|
// Document categories relation.
|
|
$categoriesIds = DocumentCategory::pluck('id');
|
|
Document::query()->get()->map(function (Document $document) use ($categoriesIds) {
|
|
$document->categories()->attach(
|
|
$categoriesIds->random(rand(1, 3))->all()
|
|
);
|
|
});
|
|
|
|
// Document tags relation.
|
|
$tagsIds = DocumentTag::pluck('id');
|
|
Document::query()->get()->map(function (Document $document) use ($tagsIds) {
|
|
$document->tags()->attach(
|
|
$tagsIds->random(rand(1, 3))->all()
|
|
);
|
|
});
|
|
}
|
|
}
|