Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Aly-ve
Last active January 28, 2022 13:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Aly-ve/be52426ef65b65b33d800763bad1fdfc to your computer and use it in GitHub Desktop.
Save Aly-ve/be52426ef65b65b33d800763bad1fdfc to your computer and use it in GitHub Desktop.
Use Laravel's Factories with custom main namespace on Laravel 8
<?php
/**
*** On Laravel 8, factories becomes classes.
*** If your are using a custom namespace for your app, you need to add Factory::guessFactoryNamesUsing.
*** There is an example, enjoy 🎊
**/
namespace CustomNamespace\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
public function boot()
{
Factory::guessFactoryNamesUsing(function (string $model) {
return 'Database\Factories\\' . class_basename($model) . 'Factory';
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment