PHP code example of offline / oc-seeder-plugin

1. Go to this page and download the library: Download offline/oc-seeder-plugin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

offline / oc-seeder-plugin example snippets



// plugins/yourvendor/yourplugin/factories/YourModelFactory.php
namespace YourVendor\YourPlugin\Factories;

class YourModelFactory extends \OFFLINE\Seeder\Classes\Factory
{
    /**
     * Default model attributes.
     */
    public function definition()
    {
        return [
            'name' => fake()->name,
            'number' => fake()->numberBetween(0, 6),
        ];
    }

    /**
     * Define states: Override attributes that differ from the default definition.
     * Use it like this: YourModel::factory()->withHigherNumber()->make();
     */
    public function withHigherNumber()
    {
        return $this->states(function(array $attributes) {
            return [
                'number' => fake()->numberBetween(100, 900);
            ];
        });
    }
}



// plugins/yourvendor/yourplugin/models/YourModel.php
namespace YourVendor\YourPlugin\Models;

class YourModel extends Model
{
    use \OFFLINE\Seeder\Traits\HasSeederFactory; // add this
}

public function registerSeeder()
{
    \YourVendor\YourPlugin\Models\YourModel::factory()->count(50)->create();
}



namespace App\Factories\Blog;

class PostFactory extends \OFFLINE\Seeder\Classes\Factory
{
    public function definition()
    {
        return [
            'title' => fake()->sentence,
            // ...
            'is_enabled' => true,
        ];
    }
}



namespace App;

use OFFLINE\Seeder\Classes\Factory;

class Provider extends \System\Classes\AppBase
{
    // ...

    /**
     * @param $seed \Closure(string $handle, \Closure(Factory $factory) $callback): void
     */
    public static function registerSeeder(\Closure $seed)
    {
        // Blog\Post = blueprint handle
        $seed('Blog\Post', function(Factory $factory) {
            $factory->count(10)->create();
        });

        $seed('Blog\Category', function(Factory $factory) {
            // ...
        });

        $seed('Blog\Author', function(Factory $factory) {
            // ...
        });
    }

// Old
factory(YourModel::class)->make();
factory(YourModel::class)->states('special')->make();
// New
YourModel::factory()->make();
YourModel::factory()->special()->make();

// Create a model
$myModel = \YourVendor\YourPlugin\Models\YourModel::factory()->create();

// Attach an image
$image = \System\Models\File::factory()->make();
$myModel->image()->save($image);

$tiny = \System\Models\File::factory()->tiny()->make();
$hd = \System\Models\File::factory()->hd()->make();
$huge = \System\Models\File::factory()->huge()->make();

$randomType = \System\Models\File::factory()->file()->make();
$pdf = \System\Models\File::factory()->pdf()->make();
$mp3 = \System\Models\File::factory()->mp3()->make();
$xlsx = \System\Models\File::factory()->xlsx()->make();

// Build a simple backend user.
\Backend\Models\User::factory()->make();

// Build a superuser backend user.
\Backend\Models\User::factory()->superuser()->make();
bash
php artisan seeder:init
bash
php artisan offline:seeder --plugins=Blog\\Post

php artisan offline:seeder --plugins=Vendor.PluginA,Vendor.PluginB --fresh