Skip to content

Commit

Permalink
Merge pull request #361 from sunxiaoguang/call_factory_first_once_per…
Browse files Browse the repository at this point in the history
…_request

[perf] Call Factory::first once and cache the result in FactoryServiceProvider
  • Loading branch information
SMEWebify committed Apr 25, 2024
2 parents 89c956a + 7632427 commit 45ac897
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app/Providers/FactoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class FactoryServiceProvider extends ServiceProvider
{
/**
* The first factory
*
* @var static|null
*/
private static ?Factory $firstFactory = null;

/**
* Register services.
*/
Expand All @@ -21,10 +28,10 @@ public function register(): void
*/
public function boot()
{
//view()->share('Factory', Factory::first());
self::$firstFactory ??= Factory::first();
//view()->share('Factory', self::$firstFactory);
View::composer('*', function ($view) {
$Factory = Factory::first();
$view->with('Factory', $Factory);
$view->with('Factory', self::$firstFactory);
});
}
}

0 comments on commit 45ac897

Please sign in to comment.