Learn How to Use Laravel Pipeline: A Step-by-Step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to another tutorial my name is William in this tutorial we are going to look at laravel Pipelines in laravel pipelines are used to process an input through a series of stages Each of which performs a specific operations on the input data the output of one stage is passed to the next stage and so on until the final stage is reached pipelines are often used in laravel for tasks such as data validation data filtering data transformation they are useful for processing complex business logic that involves multiple steps we are going to look at a very simple example over the implementation of laravel pipelines and this is just to make our tutorial very small and in this case easy to understand but I'm going to do a simple manipulation of a string and then we are going to just learn the various things that you can be able to do uh here and there and then we will be able to finish like that and you can be able to apply in your application so I would like to just uh do something on web dot PHP in the routes folder here so this is where I'm going to just put my code in this case probably if you are working on a complex uh business logic probably you have uh some of these things in a controller and then you'll be able to create classes but for the first example I'm not going to create any class I'm going to put everything here and we're going to use closures to just be able to manipulate whatever it is that I have so we are going to have a a route and we are going to have it as a get route and in this case I'm going to college pipeline test like that and then of course function and then inside here we are going to just have it like that so inside this function for the route we are going to manipulate some data and in this case I want to just have other data a variable called my text which we are going to um manipulate so I'm going to just use the pipeline and before we do that we have to import pipeline up here so let me just do that so we can say use illuminate fast threads support facets and then in this case pipeline and then after that then we can use it here so we can say pipeline and the first thing that we do we send so pipeline send and in this case what we are sending is our data so in this case the data that I'm passing through is uh just the word hello world so in this case you can be able to also send an array of values here maybe a a variable that has stored some values but in this case we're just going to send text so hello hello world then we are going to manipulate it so then you are going to have um you're good we are going to send it through so here is where we have our pipes so through like that and then now inside here it accepts an array of values so we can send this data through various pipes and here I'm going to just use closure so we are going to have a function so this is the first one let me let me let me just space such that it's very readable so function and then maybe whatever it is that you have passed in of course is a string and let me call it greeting so I'm sort of using a different variable from my text such that we'll be able to put it in the end and then we will also be using a closure and then we have next and as you can see this is a very similar to what we usually have in a middlewares so the first thing I want to do is to manipulate what we have in greeting and actually what is being passed in greeting is actually hello old so this is what we have here so greeting and I want to modify and I want to as you can see here we have the small letters in small letters for the word hello the first letter and then the world also the first letter I want to just transform it into a capital letter for hello so in other words capitalize each word and in lateral we can be able to use the Str ipper and you just say Str title title and these we actually be able to manipulate our greeting and be able to give us to give it to us in a manipulated way in this case as um hello Capital world I mean capital letter for H and then wild capital letter for world and then we go ahead and um now after we have done that we are going to say return next and then we are going to pass in a variable greeting again to the next uh pipe so in this case we have performed some manipulation here and then now we are going to pass it to the next stage where we are going to do something else and in this case I would like to just um go ahead and just copy what we have here I'm just going to copy everything that we have here because whatever is that I'm going to do is also similar but now I will modify some of the things that you're going to have so you have a comma in this case that would be our first pipe Force per se and then in the next one we will uh be doing maybe addition of an exclamation mark So in this case I will remove this and just say greeting and we are concatenating with the an exclamation mark just like that so eventually whatever it is this new greeting now will be hello world but it will be having an exclamation mark at the end so that is what we are passing it to the next uh pipe and then of course in the next pipe I would like to change the greeting now because we have in the greeting we have hello world and I want to change the word world to maybe my name I can change it to William so that is what I would like to do here so let me just uh modify here and we are just going again to use the Str Helper and here we are going to replace so let me let me just delete this so you're going to say Str replace the word world and of course it is the one that is this actually capitalized world and I want to replace it with William with the word William and where we are checking to to do the replacement is actually in our whatever it is that is stored in greetings so so you check in greeting and find the word world and replace it with William like that and then uh that will be everything about it and then now after we are done with it through so because when we put uh when we have we say through then these ones are pipes then after that we can be able to say then so here we can say we can chain and say then and then now um what in this case we are going to do is sort of like just spit out whatever it is that we have so we can have a function and um whatever is that we're having is a string again that is a data type and the string is stored in the variable greeting obviously we are going to set it to greeting like that and then afterwards we are just going to do what to Echo to Echo my text or in other words DD my text like that and see if we are actually going to have hello William and as you can see we seem to have a problem yes because I didn't put a comma here yes yes I think that one is so corrected so let us go ahead and see because my uh aravel application is already running so let's go ahead and see what we can be able to do if this manipulation is actually happening so I want us to just have localhost 8000. and as you can see our application I think you'll be running it's taking a while I do not know why let me press enter one more time uh huh yes a lot of application is running so I want us to visit our method in this case we have called its pipeline test so I will copy this and maybe we can just have like that so you copy it and just have it here pipeline test like that and paste it is loading and you can see it says pipeline class pipeline not found so let me just check again how I have been able to import because I think that is where we have an issue yes I can see I have mistyped something here I've written actually my own things pipe line like that and I think that's the reason why we were having the error so let us go ahead and refresh again and you can see we have hello William so in this case we have been able to manipulate the word hello world through several pipes the first pipe we have been able to just capitalize the the the words each word and then afterwards we have been able to add an exclamation mark they've been able to add the exclamation mark and then finally we have been able to replace world with William So eventually we have hello William something of that sort I believe uh that is a understandable I would like to just be able to do a little bit more because in this case you cannot be able to actually use it in a web.php for example this code probably you will have it in a controller but you can also each pipe can also be transformed into a pipe class so so I want us to do another thing which is very similar to what I've just done there but now in this case we are going to involve the same code but we are going to involve the pipeline classes so in this case it will help you I will just do it in the web.php but in this case of course putting that code in the controller is not very difficult but then it you give you an idea of how now you can be able to do it in your application because you can't do it exactly like this so let us see what we are going to do so I'm going to create another route and we are we are going to have it as a pipeline test two so we're going to say route get in this case uh pipeline test pipe line pipeline test two so similar to the first one and we're also going to have a function like that and inside here we are going to to have our data so here I want us to actually be able to go and create some pipes elsewhere and then we are going to use them so um let us go ahead and do it in this case I will still do the same thing we can be able to to have um again my text like that and then which is going to to use pipeline pipeline f I've typed it wrongly yes pipeline and then here we are still going to pass the same text so send the text is a hello world and then after that we are going to send it through in this case now so you have you chain it and we say through and then inside here I'm going to put pipes which I haven't created so the first one I'm going to say up where we are going to create it so yeah just sort of like looking in the specific folders We are going to create a folder called pipes eventually I will do that and then the first one I'm going to call it title so you have a class which you call Tito which you do some manipulation and then we will have let me just copy these ones and then the next one where I'm going to call it a punctuate because we are doing some punctuation to this chord to achieve the same thing so punctuate and then the third one may be replace like that so I'm going to create an app pipes I'm going to create three classes that is title punctuate and replace so that is what I'm going to do right now then of course let me just finish this code such that when I'm going when I go to create then every other thing you'll be fine so here I can I can still say um then we can chain then the same way we did in the first one so function and what we're having is a string and uh what we are calling is creating and um yes so like that um yes like that and then we set it to greeting and then at the end of everything we are going to say return my text or maybe DD my text whatever it is my text like that so as you can see there is a fundamentally very few differences between the code that you have in here previously and what we're having here and um of course we can also be able to just namespace this app pipes up up then be able to just use it use them like that but now let me go ahead and create those uh a new folder so the first one is new folder and I'm going to call it pipes that and then inside pipes I'm going to have three classes so new file the first one I'm going to call it title.php tighter dot PHP the next one I'm going to call it um new file I'm going to call it replace dot PHP I think that is what we have dot PHP and then the third one I'm going to call it Punk is it punctuate or punctuation let me see punctuate yes so the third one I'm going to call it punctuate .php new file to it dot PHP I hope my spellings are okay and inside here inside the function.php I just want us to have the PHP tag just like that and we are just going to name space we are going to name space up and uh pipes just to that and then we are going to have class a class in this case our class is punctuate we are going to have it like that inside this class we are going to have a handle method and we will pass in our string of a watch in this case we are calling greeting I'm trying to do something similar to whatever is that we have done with a previous example such that we it can be able to assist us then we can have closure and next like that and then inside here we are going to have calibrases and if it is punctuation we are just changing greeting and we are just adding the exclamation mark So in this case greeting concatenating with the exclamation mark like that that is the only manipulation that you are doing and then we are going to go to the next request so return next return next greeting in this case to we pass it to the next pipe or maybe depending with how we have it so I think this will be what you have here in punctuate and because whatever is that you are doing is very similar I would like to copy everything that we have here and I'm going to paste in the other um files so I'm going to copy and I want to paste it in title.php and here we are going to change the class name to Tito and we're also going to change whatever is that is happening here actually here we have said we are going to just use the Str uh Helper and title so in this case to just capitalize each word and then what we are manipulating is actually whatever is being passed in in the variable greeting and I believe I have typed it properly so that is what will happen here nwcif I also want to go to replace and I will also paste this same code and here the class is replace and the inside here I'm going to do what we were what were we doing we were replacing award in greeting so in this case we are going to have Str AP replace I believe STL replace and then what we are replacing is world and we are replacing world with the name William and we are replacing in uh whatever is that is being passed in the variable greeting I think everything seems to be fine everything seems to be fine so now I will go to back to web.php and just mention something so these pipes are going to be processed in this order so if you have if you are working with a data that has to be processed in a specific order then you have to arrange your classes your pipe classes in space in that specific order because there will be processed sequentially from one sequence I mean from the first one to the second one to the third one like that so I would like to just also attempt and visit pipeline test two and see what happens if everything is okay or if we can be able to manipulate that data and obviously I'm using a very simple example for purposes of uh just making you understand but in this case if uh you are working with it then you will have to maybe do it uh you whatever it is that you're doing will be a little bit complex it says pipes SDR not found so in this case there is somewhere and this is a title in title in title we have not be able to we have not imported Str so here I will say use Str and then now you save and also I think in punctuate we are not using this tier in replace yes also in replace we will also say use Str and why we are saying that is because of this because we are using this helper here so again I want us to check again and see so when we refresh it says uh undefined variable grating [Music] and Define variable greeting let me just check because I think there is uh there must be something that has been mistyped somewhere let me just check you can see here I I typed greeting a little bit long so I do not know probably that is the reason why we're having an error let me check again so we will refresh and Define that variable greeting yes now now I think it has been propagated everywhere because we copied and pasted so you can see also this one is three uh E's and also I want to check punctuation we also have three E's my bad so after that then I have corrected all of them I think I have saved yes for all of them so when I um again click there you can see Hello William uh thank you very much for following this tutorial up until this point and what we have actually been able to note is that um pipelines can actually be able to assist us to to do um uh uh to to be able to process code that actually comes about sequentially and I do not know if it's a case of over engineering or whatever but I believe uh laravel also uses pipelines especially if you look at middlewares you find something similar to what we have actually been doing in this tutorial thank you very much for following this tutorial video up until this point I ask that if you have not subscribed that you may subscribe and you can also like the video if it's a video that you think has been a helpful video and you can also comment and you can also uh do any other thing to be able to just
Info
Channel: LaraPhant
Views: 1,105
Rating: undefined out of 5
Keywords: Laravel, PHP, Pipeline, Data processing, Code organization, Web development, Backend development, Tutorial, Step-by-step, Exception handling, Middleware, Complex workflows, Streamlining, Programming, Development tools, Object-oriented programming, Best practices, Software engineering, Design patterns, Practical examples
Id: _1uuigiI7IM
Channel Id: undefined
Length: 26min 44sec (1604 seconds)
Published: Fri Apr 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.