Last active
May 9, 2023 12:56
-
-
Save petemolinero/f3a5dc11104cc37eb75494c5dfb3a422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database\Seeders; | |
use Illuminate\Database\Console\Seeds\WithoutModelEvents; | |
use Illuminate\Database\Seeder; | |
use Statamic\Facades\Collection; | |
use Statamic\Facades\Entry; | |
use Illuminate\Support\Str; | |
use Carbon\Carbon; | |
class PageSeeder extends Seeder | |
{ | |
/** | |
* Run the database seeds. | |
* | |
* Run and specify the quantity like this: | |
* SEEDER_QUANTITY=5 php artisan db:seed --class=PageSeeder --force | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
$quantity = env('SEEDER_QUANTITY', 3); | |
$page_content = Str::random(mt_rand(1000, 25000)); | |
for ($i = 0; $i < $quantity; $i++) { | |
$slug =Str::random(10); | |
$collection = Collection::findByHandle('pages'); | |
$collection_structure = $collection->structure()->in('default'); | |
$entry = Entry::make() | |
->collection('pages') | |
->published(true) | |
->slug($slug) | |
->data([ | |
'html' => [ | |
'code' => sprintf( | |
'%s' . $page_content . '%s', | |
'<span style="word-wrap: anywhere;">', | |
'</span>' | |
), | |
'mode' => 'twig', | |
], | |
'title' => Str::random(20), | |
'author' => 1, | |
'template' => 'layout', | |
]); | |
$entry->afterSave(function ($entry) use ($collection_structure) { | |
$collection_structure->syncOriginal(); | |
$collection_structure->append($entry); | |
$collection_structure->save(); | |
}); | |
$entry->save(); | |
fwrite(STDERR, print_r("\nSaving #$i", true)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment