Laravel Pipelines: Build an Api

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this screencast I'm going to show you how you can build a simple API using laravel and the new pipelines facade that was just introduced into layerable core just this past week so to give you some backstory I've already created a user database that has a thousand records and in this example I'm going to show you how we can filter by name email and date of birth now so we're going to switch over to laravel's documentation and what we're going to do is that we're going to copy this but note I'm gonna have to clean this up a little bit I don't like how this current documentation is written because it doesn't really show you how to manipulate the data so what we're going to do we're going to go to my user search controller which is already set up in my API routes file and we're going to paste this and this is where we got to start cleaning things up so we're going to import the facade we're going to see to it that we pass in our user model but no we're going to be passing the eloquent query Builder so that way we can build this API for now I'm going to take these references I don't want to use then I actually want to use then return and then we want to return paginate so this would be the same as if you had executed you know user paginate I mean it's literally going to give you the same sort of return and we haven't really done anything or passed any data so if we go here and we hit refresh we should have data and we do so it's a good start now we're going to start by using closures but in the end we're going to extract those out into their own dedicated class and how about in order just to make this a little bit cleaner I'm gonna I'm gonna end up creating a pipelines array that is and that way we can kind of do this cleanly so the first Palm that you have to put into your closure is essentially the payload that you're sending through well as you see on line 20 we're passing eloquence query Builder so I'm just going to type in this with the word builder so for the word eloquent down here and then the second poem has to be a closure object that we'll call next and the way this will work is that the closure that we created will return this next closure that will then proceed to pass the Builder reference through it and we refresh we should still have every all the data showing and we do again everything's progressing just fine so let's say we want to filter by let's go for the name okay so what we can do now is that I want to get access to this request object so let's pass this into our closure and I'll show you how to do this once we extract this out to a class as well so in this example we're going to say when the request has a name that we then want to then filter the query and I'm going to use reg XP just to kind of show you how easy and quick this can be and then we'll pass in that name value that's being supplied by the URL and if everything looks fine here so we'll switch back to the browser refresh and it's and everything's good so let's say we want to filter by name and let's go for that second record it says Chris so you see we we already are quickly tapping in to the result set based on a query and the filters that we've applied so now that we have this this is where I want us to start extracting these things out to a class their own dedicated class so on the left I create a folder off the app folders called filters we're going to create a class that's called by name because this filter is to do just that filter by name and I'm going to copy this and of course we have to make some slide adjustments so we're going to say plug function helps if I spill function correctly and then we're going to call this handle because this is how we have to express this in our code we're going to move remove that request that we took earlier and forgive me I have animals at home and it is a weekend so now the problem we're going to run into is that when we go to run this code we're going to run into an issue with the requests but before we do that let's bring in why is it not let's see here oh I know when okay at filters by name okay so let's go back to by name and of course remember when we go to run this we're going to run to an issue undefined variable request so now the next step we're saying well how do we get access to that request object now you could easily just say call the request helper you could do that but I tend to have my classes to where I kind of know what I'm dealing with so I'm not having to guess what what's being played with in terms of data so we're going to have the Constructor and then we're going to say protected and then we want the request so now that we've done this we can now access requests by calling this request so I'll make that adjustment there and here and if we refresh we should have data and it's filtered by Chris we could we could add another letter say Chris D right and of course the beautiful thing about reg XP is that you can do single letter or if a single single letter queries right and then if there's no thing nothing being a past it won't use the filter that's the beautiful thing because when this process goes to that next it's basically saying we're going to pass this on and then we're only going to apply this should it be true so now we sit there and say well what about filtering by email well all we got to do now is duplicate this and of course as you get an idea when you start duplicating code you can always find ways to kind of make it to where there's more of a dry approach to it but we're going to call this one by email we'll change the name of it and just like we did before all I got to do now is just change my references right so now that we have this we're going to go back and we're going to say by email and so as you get an idea you're going to be you could be adding multiple multiple filters but instead of having that this pipeline of rage is being filled with your with your own closures as you see extracting it out to its own class actually makes things look cleaner and we also know exactly what the code is doing so now we're going to go back and let's say we want to then filter by let's let's go for DOT org addresses we can do that now we can chain these things on top of each other let's say I want to go for somebody has the first name of Adrian just like that and then to finally bring it home we'll just copy this one more time and we'll do one for by date of birth and we'll call it by dob which is just a short way to name it and again using the same approach add it here and so now if I want to find people with the dot org address that has the letter a in it and now we can sit there and say has a date of birth of let's choose the year 2003. how about that so obviously this this gives you an idea just what all you can do with pipelines and how you can stack them together while also keeping your code nice clean readable and most importantly scalable so if you've enjoyed this video please leave me a like a comment share this on Twitter and I'll also post this on my YouTube channel which you can find me under the handle daltoncast see you next time
Info
Channel: Jordan Dalton
Views: 18,949
Rating: undefined out of 5
Keywords:
Id: 1RzqbiGVH24
Channel Id: undefined
Length: 8min 53sec (533 seconds)
Published: Sun Mar 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.