Last active
January 28, 2022 13:07
-
-
Save Aly-ve/be52426ef65b65b33d800763bad1fdfc to your computer and use it in GitHub Desktop.
Use Laravel's Factories with custom main namespace on Laravel 8
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 | |
/** | |
*** 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