PHP Classes

File: cli/seed_database.php

Recommend this page to a friend!
  Classes of Okanlawon Anuoluwapo   PHP REST API   cli/seed_database.php   Download  
File: cli/seed_database.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP REST API
Implement a REST API using WebSockets
Author: By
Last change:
Date: 3 days ago
Size: 1,036 bytes
 

Contents

Class file image Download
#!/usr/bin/env php
<?php
// Load environment variables and bootstrap the application
require_once __DIR__ . '/../bootstrap/bootstrap.php';

use
App\Models\User;

// Example function to seed the database with user data
function seedUsers() {
   
$users = [
        [
'fullname' => 'Alice', 'email' => '[email protected]', 'password' => 'Test@01'],
        [
'fullname' => 'Bob', 'email' => '[email protected]', 'password' => 'Test@01']
    ];
   
    foreach (
$users as $userData) {
        try {
           
$user = User::create($userData);
            print
"Created user: {$user->fullname} ({$user->email})\n";
        } catch (
Exception $e) {
            print
"Error creating user: " . $e->getMessage() . "\n";
        }
    }
}

seedUsers();
print
"Database seeding complete.\n";

// Set Execute Permissions: Run the following command in the terminal to make seed_database.php executable:
// chmod +x cli/seed_database.php

// Run the Script: Now, you can execute the script directly from the command line:
// ./cli/seed_database.php