0

I have a custom service provider in which I am accessing a model in boot(). But when I run php artisan migrate, it shows the below error:

[Illuminate\Database\QueryException] SQLSTATE[42S02]: Base table or view not found: 1146 Table '********' doesn't exist

I found that if we add if (!app()->runningInConsole()) { inside boot(), it works successfully.

This is the code we have used in the service provider:

public function boot()
{
    $this->bindCurrentPartToNav();
}
private function bindCurrentPartToNav(): void
{
    $currentPartName = \App\Http\Helpers\Part::getPartName();

    view()->composer(
        'includes.partials.part',
        function ($view) use ($currentPartName) {
            $view->with('currentPartName', $currentPartName);
        }
    );
}

Helper file:

public static function getPartName(): ?string
{
    return PartModel::PartKey()->active()->pluck('name')->first();
}

Model:

public function scopePartKey($query): Builder
{
    return $query->where('identifier', config('env.PART_KEY'));
}

Is there any way to remove that service provider from php artisan migrate so that we can remove runningInConsole() check in each refresh?

Thanks for your help in advance. enter image description here

chithra
  • 756
  • 7
  • 12
  • if your code depend on the session level, the it should not be in your model level. keep the C out of your M in your MVC application. (yeah broad question leads to broad answer) – N69S Jun 22 '22 at 11:01
  • @N69S It doesn't depend on the session. I want to get a name from the table based on the env value in the service provider. – chithra Jun 22 '22 at 12:16
  • well if the name depends on if the call is coming from frontend or CLI, then it depends on the session. Share some code of the boot() method you're talking about and we might help you. – N69S Jun 22 '22 at 12:24
  • please [edit] your question and add the code there, it's unreadable in the comment section – N69S Jun 22 '22 at 13:42
  • So, in your Model boot() method there is a call that to a view composition (which might also have some session dependency). Keep your V out of your M in your MVC. I still dont see why this trigger an error about migration... Your question needs more debug & details. – N69S Jun 22 '22 at 16:07
  • I have updated the question with code in helper and model. When we set up the project, the migration will not run because it throws an error because of the missing table. – chithra Jun 22 '22 at 17:54
  • It will work with(\Schema::hasTable('table')) check. But this is not a good solution, I think. Any help would be appreaciated. – chithra Jun 22 '22 at 17:55

1 Answers1

2

As any environment configuration, in your case a general configuration, you should assign a default value fall back.

public static function getSectionName(): ?string
{
    try {
        return SectionModel::sectionKey()->active()->value('name');
    } catch (\Exception $e) {
        return null;
    }
}

This will simulate the case where the section model with that specific identification_key is missing in the database.

This will also prevent any issues with the initial migration.

But in the end, you tied a model with a view rendering code. You should find another solution to dissociate them. For example, you can move the boot() code out of the model and link it to a middleware.

You can also use Singleton pattern (since it's like a general unique config across the application)

N69S
  • 16,110
  • 3
  • 22
  • 36