How To Use Laravel Multiple Database Connections

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
first installation of blood a bit and this is output your gate so it is to remind each about multiple databases how to implement multiple databases in large laravel application so for that first of all go to your env file if you are faced or to laravel then env2 environment file whether you define your all the key api key and in the database currents here so this is a database they see the receptor for first the database so I have to security to database called second TV and first TV which is empty right now so go to your in with file and keep that database name and credential in Mike is my username is root and password is blank so this is a credential for first database who easily drew in that I will our application in our application but in this tutorial we are talking about connecting connecting with a multiple databases for inter which is the if it when our application integrated hearts then we might have to connect with multiple databases or maybe our application is in person - or then who might get data from both and one so in that case we're gonna need to connect to multiple database so for that this copy this as I told me this is a default connection for first data default database so if you want to connect it to another database you just copy paste that and it will name that to the second DB in a kiss give the database name for you created in my case I have created two database for M second so after that so this is environment sitting so after that goes our I am go to as sorry config and then that database dot PHP now as you can see but sorry I need to change that to typical next time did you connection firstly when I sent second yeah I need to continue on to second do you connect scan again if it is the same name didn't tell you can be a issue so you gonna have to give it a different name so this is for first one this is the second horse and this is a second port and this is the second EP this is a username and this is the second part of rap so go to a database type PHP then this is a this is a bad back work of that I was setting we just usually work on dot ENB but what I have on this background is this is what's going on in the background so just copy and paste this mask you alright now change your since our definition here for example offer our DB username database TV host database this is a first one so this is a second one second TV sitting so you are this URL is going to be same but we have to sense tribal mask so let me change that huh so that will be in B DB one extra second which is our this one DB condition second and then host will be TV host second which is a DB host again so similarly now use your port database second username second password variable in here so notice that this is a masculine this is a mask you put this array name should be different otherwise you should not work so remember this credential I will provide the get up link you can download that and you can use that and later on now we have successfully created two databases understand but so now with this great model for it PHP artisan make model first will be like a customer for the mistake so what all does is it will create your magazine model and controller let me show that to you little controller there is a Moodle and there is a migration file you can create it one command so this is all so another one I need is a PSP artist and make Moodle let me call it a supplier and that is you can also use this command sorry is not working so it's taking a lot of time man so let me go ahead so in your magnificent file and you have set up a tool in database connection but how can this Meyerson will know which database you to maggot-like fossum DB or second so for that it will not work you have to make a little bit scenes in here so for that all we have to do is before this create we have to add connection connection then understand name this is the first database so I'm gonna write my skill by default it will go to our it will it will be equal to our first database so I don't have to do it anything in here I'll have to do is string let me give it a name so let me make this and show you where it goes it's sorry yes okay it's migrated right so I haven't set up anything it's the default setting so code here in DB so in second DB there is nothing in the first TV this would be a customer yeah there is your customer and also there is a supplier because I haven't defined anything so supplier it's going here because your customer there is a fill so this is differently it would work as a MySQL first connection so boat here in supplier I need this to be in a second second database now go to the second allow soku here type connection and then my skill to and also the same field like a string and there it goes beyond the name you can also give it an olive oil good if you need what is not necessary so I have already migrated so let me refer to the magazine so what migration leaflets which does is you roll back all the medicine for my question of heart and it will remind it will migrate again so now in my previous in my previous migration there was a question of Sabra also suing in customer but now there is only customer in first TB and if I go to the second TV there is a supplier so to work migration file all they have to do is add this connection so we set up our my person file but now go to a way back here to were out call the customer and supplier and customer some we're going to load in the customer now in magazine file is recognizing which the deficit to covert cost on board model we also didn't need to define it on a model so on suppliers just add this line protected to connection MySQL to with specifically database start PHP and array you created this you don't have to create for custom all because it will basically default by larval it will go through your filter connection NASA first TV so now I need to get a data data from customer right so open customer controller in here I need a data customer this elephant bath this is elephant by the way or you don't know anything about elephant you can visit manager to turrets so let me give give some data data on customer side and customers you sort first customer and then handle only second first of all they have to data in that customer so now here type a rap name database to from a six month it is neither always for a minute so the problem was this will ear worth keeping because I need to solve and introduced admire solo so we start a report then it will work out see there is a customer and data is given their first customer and also there's a Ferrari another one is second customer from my database first of all first and second so it's working so but this is it from this is a from so same to the same thing for supplier go to your wipe that PSP and make her out supplier then go to your supplier controller and do this and if I insert their time supplier for supplier gets up there and now go to your supplier it's working here so it will all work so other thing to remember is my school is taking as j4 a database this first one is taking institution for default database but while we're defining it so go up and there is a yeah the fall is DB connection - q1 if you if you need to pick mask you'll hear this database you get at the differ one just go to your there fall and shake since data in here so on supplier controller so this will work because we have defined in here in case it does not work you have to do is you have to define your connection you can also define your connection in here so for that you can supplier model is to new supplier then some line model you can define your connection here sweet connection my excuse to and you're gonna find out any data in here if you need to find do that with ID 1 of model software sorry supplier model find market so good here DV let's take supplier table there is a first apply with a delta T with a ID of 1 go to your as you can see swings of that first so in case this will not work you can define connection in here and also this is eloquent way but if you need to work with query builder what you have to do is dollar supplier DB connection my skill to remember this is from database we define the table name given a name suppliers then get Allison Diane top the supplier I'll go to your browser as you can see is working so this is how you implement multiple database connection in here sister so basic provision in the end if I define through your new database here in database table this is the default one you can define default one here then this coffee is high school and you create another give you a magical to name this so in my cursor microcell define your database name type connection type by default is define as in database and in here they fold as masculine one if there is taken as the default one so if you want to same through second terms since here now thing after is equal to your model and this connection type int connect in so controller there is a multiple of way you can just do as world boy or you can define your database type layer question type here and also you can in eloquent way and then also you can use a query builder more way so this is for that's it for multiple connection
Info
Channel: Code Snippet
Views: 2,735
Rating: undefined out of 5
Keywords: laravel, database, laravel database, database connection, laravel multiple databases, laravel db connection, laravel multiple database connections, laravel two databases, eloquent multiple databases, laravel change database connection, laravel model database connection, laravel connect to multiple databases, laravel 7 multiple database connection
Id: hPy-F7Ev9aA
Channel Id: undefined
Length: 18min 24sec (1104 seconds)
Published: Fri May 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.