WOW! Laravel's pipeline pattern is AWESOME

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning ladies and gentlemen in this episode we'll talk about how to use lights to create pipeline pattern let's get started so I will provide you with two great examples and how you could use it uh first of them is onboarding the second one is filtering requests so let's see how we can utilize them so let's say that this is an endpoint that people should only get access to after they completely inboarding because you know maybe we need to set up some data or whatever what we can do is we can obviously create a middleware because that's the best way to to do right here and you're right but you see the problem here is that if we start creating this this and boarding middleware and uh and I just created one here you see it will go like this so like we can do like user equals request user and Danny to do diffuser I know email verified uh it's not then we would return a custom response uh you know saying something that the user is Naughty Boy and so they shouldn't use the uh the platform and this would be like for free or you could create a custom status code so your phone can understand it so like 419 for example they would continue on this path so you know if the user uh has team for example um like team uh exists or we could just check team ID but that's not well it could be deleted but yeah so then we would do if it doesn't exist we would do return in a response Json and then hell over again a message uh you know needs to create a team uh you could do the same thing with like verifying that they have uh at their team members that they have proper role selected that they chose their preferences and stuff like that it will really depend on your application but you can see the problem here it will continue to get messy and if you have like six conditions and these are fairly simple that I just you know used so you probably have something that's more complex for example saying if they have an active subscription it will become Ms really quickly so in order to fix that a bit what we could do is we could use a pipeline pattern and pipeline pattern is really cool and allows you just for that basically to to like filter something through something so though it works uh is you do pipeliner uh oh it became a facade now so you send something through the pipeline in our case we'll be sending the user we need to find true and here um so yeah we sent something through something and then we basically uh return next from request because um you know we are in a middleware itself so like you can see the similarity this laravel under the hood uses middlewares uh uses pipelines for for middlewares exactly um so yeah now let's transform our our pipeline so uh the way it works is you get a function and then you get what you're sending through and then you get a closure uh that calls to the next one so again you do something like this and [Music] earring is fine you return next and then pass the user so you know here you can do the same thing I'm sorry let me just copy that I think it will be easier and uh just copy that error message and now we can remove it's bad boys and just replace this piece here Okay so it's not better you may say and that's true and that's mostly because these conditions are super simple and there is some boilerplate that's added there so the way that you can extract it and it's still better uh if you can simply create like concerns and uh or be good like onboarding and then concerns are just paste it here it doesn't matter and here you could do something like uh you know verifies email uh yeah very face email or we could do verifies that's team exists and you could simply take that code paste it here just change it public function uh invoke so we interchanges into invokable we need to import closure and that's it so it's like a middleware basically uh we can do the same thing here we need to copy this other function obviously still let's do public oh we have function already and YouTube invoke [Music] um sorry and then import the closure and now all we need to do is remove all that and we can simply do verifies email and here we had a semicolon the very end which was a problem uh and after a very face email you need to add verifies that team exists it's really nice about it is that you now don't have to remember anything about the implementation so this way you can basically build your onboarding process as step by step where each step would be its own separate class which is really handy um now if you're using like if your app is separated from the front end so like your Frontline is separate from the back that you're just using an API what you could do is you could come up with custom codes that you could pass your front ends that the front would know of and then they could communicate based on that so uh you know when this code is hits the user would be redirected there for example or you could duplicate that the logic on the front end whichever whatever works for you uh this wouldn't be obviously a case and you're just using blade or any other technology uh so yeah this is how to use it for onboarding now what you could also do let's say we have something like this and we want to have a way to to filter them so in our Emoji controller what we can do is we can start if reading a request right so let's make requests emojis MLG filter request okay let's inject it here alrighty and you know normally you do is you'd have like a query so it would be you know request validated um sorry it would be like MLG query and then you do something like if requests you know validate heads um you know category then query where category is equal to that category mentioned above and let's just assign it so category equals this and yeah that's how you'd construct your query uh you know it would continue getting more and more complex there is a couple of ways to obviously handle that but uh one of them is pipeline so what we can do so what we could do is we would create a new pipeline um so through our pipeline would send something the problem here is that we can only send one thing and we kind of need to send in the query but also need to send in their requests um so how do we deal with that well a couple of ways you can just first of all you could use a global um Global helper for their request which would work not a big fan what you could also do is you could pass an array so the first argument would be the query the second would be the request or you could just pass dto so you could have like uh where is the Emoji oh you don't have dtfs in this project I think so I'll just create like a directory um data transfer objects don't we have okay we don't interesting uh data transfer objects so like this is a simple setup and here we'll just have ADD plain old PHP class that's um will have some data so let's call it MOG filter.to and you could argue that's having a query here kinda is smack and maybe that's true I'm just showing you a couple of ways to do it you could just use the global helper uh constructs and inside of here we'll have the public lead read-only query and this will be an instance of uh query Builder actually so we'll have a builder and we need to actually name that variable and here we'll have read only um drink no category oblique read-only string name they should be Noble uh to or new and let's just say that this is all that's that emoji can be filtered by Alice comment that for a second now we need to add valuation rules obviously so category is syllable uh let's do something like that Max 50 whatever and then we had the name which is again nullable uh main three rocks 50 whatever it's not important important that we have them there um so now that we have this handled what we need to do is we need to go to our dtl and so we can create a public static function request here we would pass the MLG filter request the problem with this approach that we need to remove read-only from the Builder so like that's uh that's why I was saying that you know this this may not be the greatest use case for it because dto should be mutable uh but you could just like use an array or just don't call it dtl and call it Builder passer or whatever it's really not important and I would let's let's do it the proper way I mean it's not like great but who cares uh we'll do an array so argument holding will be query argument two will be our Emoji filter dto from requests and then you pass the request and who cares and then is it true it will release all of the filters here and then you would uh return and this would be our new query this would be Emoji query and yeah this should return a self to return itself request validated category and here we would do request validated name and now return new self obviously and now this shouldn't be a problem here so now we just need to create our filters again the result of place where you could do it um I will just add a new directory called filters and ads emoji will be easier to actually do if you like the uh because then you'd have it all in one place but that's not what we have here it's not really important and yeah let's create that uh you know Emoji name filter so this will be a public function that's invokable and here again the argument one is an array we could also do it like a universal class um yeah we could do it so maybe let's do something like um filter up and this would accept two things so the thing one would be public protected uh pillar and second one will be protected under class unless you want to add like a base cluster or dtos so you could have like a you know base dto and then this could extends base dto and then here in our where is it terrible we could just do face dto so we are sure that they are passing the dto um kinda and we can just do you know public and here we can do filterable and we could do in a query equals filterable query uh Builder and let's call it dto we can even create like a filterable instance or a given resource so you have a better type hinting but that's like I feel that we're getting to the Overkill face uh so yeah what we could do now is simply do a filterable dto uh if it has you know name then we would do worry equals query or name is the name this should probably be uh like a like and it would return next filterable query and filterable dto because we also have a closure here so this basically allows you to split your query filters into separate fields which again uh you know not a big deal it's kind of what was category right category yeah category uh not a big deal when our queries are really simple but a big deal as soon as you have like some some custom SQL or more advanced query the thing to write uh so let's send this and what we should do is do new filterable um this shouldn't be in the ring anymore all right it's it's an eloquent Builder let's feel fixed High paint and now let's send them through our Emoji name filter and through emojic category filter so we can remove this we can remove all of that and we can simply do query get now let's see if it works all right so we got an error that there is no gets on filterable that's true we should do um filter filterable and we should do a few terrible query get you could how did you call it Builder come on wait did we name it query here yeah no we didn't uh Builder okay send it now okay so it works now let's do category with Smileys and something or people and body whatever let's send that and it works we can do [Music] name is equal to weaving Kent and it also works so again this may be an overkill for you there is a lot of ways to accomplish the same behavior in a different way but if it seems clean to you if you like to use it then you can use it um So yeah thank you guys so much for watching and I will see you in the next one hope you like it leave a like below and bye
Info
Channel: Przemysław Przyłucki
Views: 10,597
Rating: undefined out of 5
Keywords: laravel, vue, tailwind, saas, laravel saas
Id: Ttfg93ZtfJ4
Channel Id: undefined
Length: 20min 38sec (1238 seconds)
Published: Mon Apr 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.