Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception for runningInConsole in FilamentFabricatorServiceProvid… #160

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from

Conversation

yolanmees
Copy link

Hello,

I've encountered an issue with this package, related to using models directly within Block files. The problem arises when these models are loaded from a ServiceProvider. This becomes evident during a fresh migration process, where the error "table not exists" is thrown because the system attempts to access the table while it is being migrated.

To address this issue, I've made modifications to the FilamentFabricatorServiceProvider. My approach involves wrapping certain parts of the service provider logic within an if statement that checks if the application is running in console mode. This ensures that the code within the block doesn't execute when commands are running, thereby preventing the Block files from being called prematurely.

Here's the change I've implemented:

- Route::bind('filamentFabricatorPage', function ($value) {
-     $pageModel = FilamentFabricator::getPageModel();
-     $pageUrls = FilamentFabricator::getPageUrls();
-     $value = Str::start($value, '/');
-     $pageId = array_search($value, $pageUrls);
-     return $pageModel::query()
-         ->where('id', $pageId)
-         ->firstOrFail();
- });
- $this->registerComponentsFromDirectory(
-     Layout::class,
-     config('filament-fabricator.layouts.register'),
-     config('filament-fabricator.layouts.path'),
-     config('filament-fabricator.layouts.namespace')
- );
- $this->registerComponentsFromDirectory(
-     PageBlock::class,
-     config('filament-fabricator.page-blocks.register'),
-     config('filament-fabricator.page-blocks.path'),
-     config('filament-fabricator.page-blocks.namespace')
- );

+ if (! $this->app->runningInConsole()) {
+     Route::bind('filamentFabricatorPage', function ($value) {
+         $pageModel = FilamentFabricator::getPageModel();
+         $pageUrls = FilamentFabricator::getPageUrls();
+         $value = Str::start($value, '/');
+         $pageId = array_search($value, $pageUrls);
+         return $pageModel::query()
+             ->where('id', $pageId)
+             ->firstOrFail();
+     });
+     $this->registerComponentsFromDirectory(
+         Layout::class,
+         config('filament-fabricator.layouts.register'),
+         config('filament-fabricator.layouts.path'),
+         config('filament-fabricator.layouts.namespace')
+     );
+     $this->registerComponentsFromDirectory(
+         PageBlock::class,
+         config('filament-fabricator.page-blocks.register'),
+         config('filament-fabricator.page-blocks.path'),
+         config('filament-fabricator.page-blocks.namespace')
+     );
+ }

With this adjustment, the service provider skips this code when Laravel commands are executed, effectively resolving the migration error without impacting the operational functionality of the package.

I hope this fix can be helpful for others facing the same issue and I believe it could be a valuable addition to the package.

Best regards,
Yolan Mees

…er.php

I've encountered an issue with the `Z3d0X/filament-fabricator` package in Laravel, related to using models directly within Block files. The problem arises when these models are loaded from a ServiceProvider. This becomes evident during a fresh migration process, where the error "table not exists" is thrown because the system attempts to access the table while it is being migrated.
Copy link

what-the-diff bot commented May 23, 2024

PR Summary

  • Modification of the FilamentFabricatorServiceProvider.php
    The FilamentFabricatorServiceProvider.php file underwent some changes to enhance the overall application function.

  • Inclusion of a new condition
    A new condition was integrated into the application operations. It's designed to confirm if the application is not executing in console mode. This ensures that certain parts of the code are only run when necessary, increasing efficiency.

  • Adjustment of code block structure
    A portion of the code block, which involves the act of registering a route bind and certain components, has been moved to come under the new condition. This alteration aims to streamline operations and ensure that these actions only occur when appropriate, fostering optimized application performance.

@matthewmnewman
Copy link

You're absolutely right. Incorporating the Page Model into any Fabricator Block can lead to failure during a fresh installation because of the absence of the pages table, which cannot be executed due to the Service Provider limitation.

It's unclear whether this fix has been thoroughly tested, but addressing it would be beneficial as it can create complications when initiating a new project with pre-existing blocks.

@TwilightDuck
Copy link

I'm running into the same issue here.
Could this possible be merged? Would help me a lot! Thanks! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants