Created
May 9, 2023 12:49
-
-
Save petemolinero/bb076a6efc3fb197a1ee830276ad4417 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\Entry; | |
use Illuminate\Support\Str; | |
use Statamic\Eloquent\Entries\EntryModel; | |
use Statamic\Eloquent\Structures\TreeModel; | |
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); | |
$tree = TreeModel::where('handle', 'pages')->first(); | |
$current_entry_count = count($tree->tree) + 1; | |
$new_ids = []; | |
for ($i = 0; $i < $quantity; $i++) { | |
$page_content = Str::random(mt_rand(1000, 25000)); | |
$slug = Str::random(10); | |
$entry = new EntryModel(); | |
$entry->collection = 'pages'; | |
$entry->site = 'default'; | |
$entry->order = $current_entry_count + $i; | |
$entry->published = true; | |
$entry->status = 'published'; | |
$entry->date = Carbon::now(); | |
$entry->slug = $slug; | |
$entry->uri = '/' . $slug; | |
$entry->data = [ | |
'html' => [ | |
'code' => sprintf( | |
'%s' . $page_content . '%s', | |
'<span style="word-wrap: anywhere;">', | |
'</span>' | |
), | |
'mode' => 'twig', | |
], | |
'title' => Str::random(20), | |
'author' => $user->id, | |
'template' => 'layout', | |
]; | |
$entry->save(); | |
$new_ids[] = [ | |
'entry' => $entry->id | |
]; | |
fwrite(STDERR, print_r("\nSaving #$i", true)); | |
} | |
$tree->tree = array_merge($tree->tree, $new_ids); | |
$tree->save(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment