Laravel 5.4 tutorials: Storing data into two tables and use of database transaction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this tutorial you'll learn about how you can store data into to table so firstly we have to make a farm and I made a form of this simple form this is where there is a title and there is a article is that there is a author description so I'll make a two table for this one is for article and other is for author description so that whenever I create this article then I can store this data into two table so first of all let's make a controller and let's make this from runnable okay so I'll make make a Moodle as well okay and then migration is miss controller oh sorry okay article okay let's make an article Moodle and migration azules controller hey this is a shortcut method to create three advanced mode oh it obvious or migration is release of controller so there is migration here you can see which name is articles just now I create this and there is a model here you can see over here there is a model and there is also a controller so let's go to the controller first unless make function cold okay great so that we can run that view okay so written view and let's use in view of articles he did great you can see that it is inside the article folder quite fine okay so let's save this list upon wave dot PHP and in wave dot PHP let's make a route it method plus article plus crate to open a crate so article control fur great okay great Nathan you're fine so we just made this and we have already major migration will come to that but firstly less around this so there is a great okay first of all let's go to the layout and sorry layout app so inside here there's our articles let's great average for this one so okay this wrong this yeah let's run this out there is shown here and there is a great new article so whenever I open this you can see the articles that's great business Ronnie no so all store title article and the author name author address author country this term name author address and author continue the next table and the title and article into the all the tables so first of all to restore this let's make a table let's go to the ok editor and into the migration who is migration okay yes okay migration here so here is ID so if you want to make if you want to make there is a title so I ever told you don't do good other field in this then you can right now and it's just spitting to table that is a string and other weights article and not only that I also have to wait articles so there will be a user ID in here so put it here integer with where user ID will be in integer so put integer and you can simply put here which is user ID okay and user ID will be in unsigned so not forget to put this and the next one is let's make foreign relationship between the articles and the user ID so how can we make the foreign relationship here so for that is well we have to put a table and just put the what fo re is in foreign okay okay for it and instead that you can simply put there for which column you want to make a foreign here so the column is user ID over here and this is references are EF ER en si es references okay and the references will be on what that means our reference will be an ID of users one user one so there will be ID over here because there is a increments of ID in here so now there will be references to the ID um on users table so let's put eight users there he's just not table here and you can put a cascade over here like you can put on update hey you can put a cascade over here and if you went to put on delete as well so it's very simple like you did just now like on orbit you can also simply adhere on delete and you can put here gasket okay so never forget this so this one is articles so articles is are related or articles is giving a foreign relationship or having your relationship with our user on the same way let's make other table or other migration which name is also called like like Auto Detail okay so let's go to the here and let's make PHP audition make migration or we can simply create if you want to put of in feature if you want if you think that we can put all different kinds of other names of those things then you can simply make a different model for this as well because there will be different kinds of things in in this migration in this model is so if you want them we can make its controller and all those things otherwise simply we can make migrations so okay let's make a migration first of all hey so great let's put the name Autarch hey and then evil okay so whenever we'll put it we'll make this thing you can just simply press enter for this and you can see here for this okay so just if you want in this you can copy this one and paste it here because it's almost similar okay so here is the article so remove articles and put authors Hey and okay IDs okay fine and this is put your name and as you can put your what do we have prayed over here like our name and our country address okay fine it's just a strength and then we can put the country here and then on this in the same way in the same way we can copy this one and again we can paste and we can put this name is address okay so here is an integer and here is written a user ID so we didn't have to make a release in this authors with users because authorship in listen with the article because article will have other so let's make this with the articles of article ID so a rtic article ID okay we can put this on this way and this is of course article ID article ID and our friends with our ID but with the articles okay and others just seen some fine now let's get it here okay fine there is the article and all those things are fine now let's put it in more do the one after Chafee there is sorry okay here is a model and in this model first of all refer to the table okay and the table name would be articles for this and then fillable okay so here we a table name for which you filthy here so in articles there are title an article and a this is your ID so you can put title okay and article and we can also put user right you know you all the same way we can go to the okay there's no model for that's so let's put your author hey you teach you author okay so after this let's copy all content of article endless beauty the outer sorry and let's go to the author one and then the space - okay oh sorry health or yes and here is Otto's au TSP ourselves right so and in authors you thos of the second name contrary address name and then country and then address and then article ID so this must and then don't forget to put this in here because there is Mt finally we have made a formalism cheap and we have made a table of authors and articles here so late my grid this one and we'll gate evil over here so PHP audition and my good right so I think we have done everything here okay fine we have migrated articles table and is relays authors table hey so if you're good to hear and in PHP my admin yes then in inside this you can see that in the blocks there is authors sooo lies articles the table has been already created so now let's go to the article controller and inside here now we'll be starting our real code so that we can store tape or data into the to table and how we can store these things let's go to here let's make a constructor first of all let's make tailor article okay here and we have Auto here this fine now okay let's put article right and on the same way we have a car that's right hey teacher okay fine okay first and everything is fine I can find no let's critic control gain and we have made this great now we'll make a niched function which is which is called store to store the date okay so this one well Krait here so first of all there's the one thing you must make okay let's make a huge here and we can write here and for this you know dollar we are creating articles out of this article okay so okay the question is barbecue over here this video will be too long so I won't do any validation over here okay so I'll be directly I'll put all the data so in the article there will be a title and we'll put a title from our IQ all title okay and then we'll put article okay put your article down awake article okay so and the user ID and the user ID we were simply called fine and then we'll have great articles on the same way we will be trading for we'll be doing the same thing same stuff for same thing for author so you teach you all right so and we'll insert a thorough name over here right and then author maybe country yeah so we'll put your other country and the next one is address so we'll put the same we address right right and then we put here article ID and the ID will be polite so it's too easy to do but the main thing is that it's very easy to do over here but the main thing is that if we'll do on this way then sometimes make great a problem so for this we have to do our transactions so the transaction is DV transistor never forget to do da da do do do it right there some neighbor forget to put here use TV transitions and if it cannot break your internet when you succeed this more than Ito it's true article but if wildedge toward the author over there so this make great problem in your products in feature so for that never forget to put every transaction transaction article and we can do something like this if it's happened then yeah we can do on this way okay on this way we can do okay let's let's do on this way so now if we find that there will be if the article and author is fine thing the Rovia commit otherwise unless you can put your else otherwise we can say that there will be a DV rollback and so it's not a big deal to do these things okay so if we if we do on this way then of course we can get in this oxide so let's go to the forum site okay we have called in to store so as good to hear and in the action let's send this absent article controller into the into the store okay and they just share sort of token post and everything it's fine there is a title okay okay there is no any name over here I title and there is a author name so for the other name which the name of what I have done over here for name for the author name I put an underscore name right so let's get the crate here in their name so there is a certain oh and in the name so for this I put this country so over here and again the same way the other address I have put in here Oh ad so so and there is text today for the article so now we can do one thing for the article here I've done for the article are simply article quite find so here is article some and some eat again everything is fine now okay now everything is fine over here so we'll go to the wave here and there is no any pushed let's make our out this and let's put a post method okay so slash article last okay okay let's make us article on me so article controller let's put stroll over here so this is a post meter now now finally we have succeed of making these things and now we will be saying that is it working fine or not so first of all we'll run the program okay so here is a program so let's run this one oh there is your problem we're here article controller on the line number 200 so let's go to here in the article controller and the line number 12 so vegetable here with the two of you fine fine everything is working fine so let's go to here okay let's put a title of mmm Jack hey let's put your article one band okay look you just put one band yeah name is a check okay Sun and country let's put it California yeah sorry country is you earthly and the address okay California let's put only California that's not for others and then article let's put article let's copy this one here yeah and then this this over here yeah so and what happened over your listing Oh fine oh they did too long for column article okay now it have written that data too long for column article we can simply put a long over there so but for now let's not do that okay because we have already migrated their things I'll teach you how to how to do that as well check I would like to see that is it working fine or not so okay Laureen okay let's put only this one and then this okay so okay it's not doing anything because and both side we have told we're told that you tie it so let's see here is it working or not oh yeah it's working right because it has sending one band and lorem ipsum okay and in the author's name it has written a Jackson and USA California fine that means we are successful of storing in the both table okay so we will now solve the next previous hero that is saying that there is a long data so how we can store a long dinner because article can be a long so if we have written here strain you can can know that in the only articles we can see here there is an article in E string so I mean it's there will be a long text okay fine so we can simply write a long text over here so in the case of takes list again this again drop out this all things okay PHP polishin okay migrate roll back if you'll roll back this then we can see that there is only a rollback of articles and authors because on the previous things will be rolled back okay whatever we're migrated so now we'll again migrate the same things that means now we have migrated is attached so now again will we we will rifts re we will refresh these things okay so now let's refresh this let's put a one band let's put here Jackson okay Jackson oh you can see okay there is no any teacher now so he may be confused at that time okay so there's no any data okay so other countries USA and let's put the addresses of California and let's copy the same thing okay let's copy this two times okay so now I like to put this like a let's first one then oh now it is successfully posted so now it won't say that of data is too long okay because if we have put text over there then we can store long text as well okay fine now we can see which format isn't is this so this is in text oh where is it this is the text type so there is a text type as well in the database so we can use text in that case so thank you so much for watching this tutorial I know that these two long tutorials already Ettore meanors of tutorial so I've taken a lot of time of this but I hope that you have understood this clearly that how you can store data into two table and what you have to use on that case a must've most important thing in this is don't forget to put database transactions because if you don't put big intrinsic something if sometimes it crafts in these things then it may create a lot of problem over here okay fine but that I can show you a simple here how it can crash your data okay so let's put your hand right there is not an article ID card corner right okay let's put it hundred and I'll what I like to comment this begin transaction I don't want to put this begin transaction over here I want to put you roll back over here I want to put TV comet over here and I don't want to row back here okay so the most important let's comment this all okay so I directly I like to really understand so what has happened if I'll do on this kind of things thing you don't make your come way to it will make you more difficult because subsection same thing okay a third country you SLS with this and then author address California it's them let's let's post this oh no yo rattle it no iterator but let's see this toy in here I don't know how it's been here there is no any over there and there's others and you have as you can see there is an article idea found right now the main question is here we have written the article idea on bread but is there any article idea will here see ya I don't know that's one the mistake but I can find this I have coded by myself so it has done that kinds of error okay sometimes if it doesn't come in this case that whatever happened yeah similarly on that case I am showing it but let's put one thing if there is no any article okay let's put article and I don't know there is no in this kind of over there okay so but just just in the case okay I'm just showing you that if only article sometimes this may be I mean to say that sometimes this may be successful of restoring but this may not be successful while stirring so what happens in that case in let's put us over here let's okay California or a fine let's first okay in this case you got the ero okay but if one do even though you have got the ear over here when you account to the articles then you can see that it is already stored over here right this already stored and whenever you see the authors there is no any author in ID three is that article 83 no so in this case it gets a lot of problems because whenever you want to whenever you try to find these authors who have written this if you want to try to find this author then they won't be any author so in that case there will be a hearer for them because there won't be any author so in this case but in a system you must store the author name and whenever you try to return the data from other table then you get such errors so remember while doing this kind of things so I will tell you this because it is yes you know for me as well so and again I want to delete this and this because there is no author of our article to an article 3 so okay fine thank you so much for watching this tutorial and in the next tutorial I'll teach you how we can drill track the data from the 2 t1 and so then in a single view or single table like in one table there may be article title and author name and author describes all those things can happen there are published state or all those things so in next tutor also either how you can take the data from two three four and so that thank you
Info
Channel: Kapil Parajuli
Views: 9,189
Rating: undefined out of 5
Keywords: laravel 5.4 step by step, laravel tutorials, database transaction, how to store data, how to store data into two tables, how to store data into 2 tables, ACID transaction, ACID in laravel, transaction in laravel, storing into more then one table in laravel, store data in laravel, mysql, phpmyadmin, storing data into more tables laravel, laravel best storing concept, foreign in laravel, how to show relationship between tables in laravel, store data into phpmyadmin
Id: GxitqHRs8BA
Channel Id: undefined
Length: 31min 49sec (1909 seconds)
Published: Fri Aug 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.