Skip to content

Instantly share code, notes, and snippets.

@PeterDKC
Last active December 19, 2019 17:28
Show Gist options
  • Save PeterDKC/b2cc17d1e56b359cbcbe3e8d319f87b3 to your computer and use it in GitHub Desktop.
Save PeterDKC/b2cc17d1e56b359cbcbe3e8d319f87b3 to your computer and use it in GitHub Desktop.
Extend a Laravel Model Factory
<?php
// class Admin extends User
$factory->define(App\Admin::class, function (Faker\Generator $faker) {
return app(Illuminate\Database\Eloquent\Factory::class)
->raw(
Package\Models\User::class,
[
'foobar' => 'barbaz',
'bazqux' => 42
]
);
});
@PeterDKC
Copy link
Author

If a package defines a Model Foobar and you class MyFoobar extends Foobar, you probably also want to create a factory(MyFoobar::class) for any fields that your child Model provides or anything that overrides the parent Model's fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment