Laravel Service Providers: One Typical Mistake

preview_player
Показать описание
I want to show you a mistake that I did myself pretty recently: if you define some logic in your Service Providers, make sure you double-check that DB table exists.

Related videos:

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

As a sidetip for anyone watching the video, there is the 'php artisan migrate:fresh' command which drops all database tables and then runs the migrations again. So you don't have to drop all tables manually ( 3:17 ).

nteath
Автор

Does Schema::hasTable() run an extra query? In that case I prefere the try/catch.

JNL
Автор

If you use RefreshDatabase in the tests this can also cause problems.
The provider runs before the refresh runs.
So you get then for example always a 403.
So you have to define the gates in the setup method.

Stoff
Автор

As usual: informative, well-described, short, and straight to the point
Thank you very much for the effort you put into your videos

EyadMohammedOsama
Автор

Insightful. I wonder about whether it's a good idea to put a dB lookup (two of them) into a service provider boot method since won't it cause those two queries plus associated overhead for each http request, to be executed for each request? When many of them won't it? Thanks for all the good videos.

OnlinePseudonym
Автор

How about using Gate::after/before instead of hardcoding a Gate::define for each permission?

You can register a function that runs after or before explicitly define gates, and it can check permissions or 'abilities' dinamically.

One thing to mindful of about gates in general is that they only run when no other gate before them has explicitly allowed or disallowed the given request. Gates can return true, false, or null, and only the null value would give the chance to the next gate to validate the user's authorization. For this, the Laravel docs have some guidance in the documentation page about whether you should use 'after' or 'before'.

Note: This is not an original idea. I have seen it before in other packages, and I just wanted to share it.

UndadCat
Автор

I wish there was a Povilas Korop for rails too. Fantastic bro. I had this problem just a couple weeks ago. It's not the first time that happen. I have a problem in my project and couple days/weeks later you come up with video explaining that problem with solutions. Looks like you spying on me ahah.
Good job Povilas. Appreciate it. Everybody does.

MrDarioio
Автор

This approach seems to hit database all the time whether the code needs to use permissions or not, for example displaying login page. I wonder if it is better to register permissions in a middleware and use the middleware only on the routes that really use permissions.

alexrusin
Автор

So what happens after the migration? Do the gates eventually get registered? I personally have a similar problem utilizing helpers that reference a table to pull options for various dropdowns on my site.

When I first load my app for the day, I get that table is not found, but when I refresh, it all works. Probably don't have it set right, but for now, I'm just dealing with it.

jcc
Автор

Yeah I notice this in your laravel+vue3 github repo. And I got the same error. So what I did, I just commented authserviceprovider when I trigger the migrate artisan.

phojie
Автор

Querying database in Service Providers will make image build fail in CI/CD - Kubernetes deployment context, container is built before accessing to credentials from namespace configuration

julienSibille
Автор

how about App::runningInConsole() instead of schema?

anwardote
Автор

What extension you are using to format the code when saving?

leonuneza
Автор

Your videos becomes my every morning pre-work routine.
One question, which theme you use in editor?

stefanilic
Автор

This error also comes if we are sharing global data also in blade. Using auth service provider

NotBeHaris
Автор

Try catch is what you want. It'll perform better and more appropriate in this case. You don't want to keep on checking the db if the table exists. This is the reason why comp sci degree is important!

truejayoh
Автор

sir i am facing problem defining gate it always returning false
and can we pass any message inside inside gate

shocchosolutions
Автор

Wait. It turns out that this policies does not registered in provider? How make this register again?

ДмитрийРогожин-нд
Автор

Hi @PovilasKorop

Can you help me to understand AWS SQS with Laravel 8.
Basically I have installed aws-sdk package into laravel and configured AWS SQS configuration details into .env file.
But I am getting error of "MessageGroupID" is require while sending queue into AWS.



Q : Why I am using AWS SQS Queue ?
A : Because my application is linked with many other projects ( ie. Python into another EC2 ) and so on. Using SQS will help all team to fetch relevant message and further execute some python actions.

jaydevgodvaishnav
Автор

Problem is if that table doesn't exist and the app received a normal web request there would be 0 permission limitations. it "shouldn't" happen but maybe user error on deploy could cause the DB to be in a state where it is missing that one table, for example you add this to an existing app with an exist DB but forget to run the migration to add the permissions table. I wonder if you could instead check if its a web or an artisan request. Perhaps even more ideally what artisan command, to only skip on a migrate command.

ryanb