 Hi, this is Christian welcome back in this episode. We're going to do the model part 2 this time We're going to render data from a database to our application Okay, so before I start just a little bit house cleaning house keeping here on the portfolio page You'll see that I fixed the buttons here to look a little bit nicer. I had a incorrect Syntax for that also change the color here for to make it green Add another button here to delete all of them. I suppose do a single one Okay, so let's take a look and see what I did first before we proceed. So here is the portfolio blade page I added a link at the top and Basics is going to delete as well with no ID by the way as opposed to the ones we have on the bottom You pass an ID, okay? And and so I put it here make it a danger and this is changed to a success and I float that right Okay, and then on the bottom here It was initially incorrect. I put the word small, but it's just SM for that and I changed Yeah, I think that's pretty basically. Yeah, I changed the color to a secondary in danger. That's just all I did for this one Okay, so nothing changes here. All right, so that's good go so here is what I did for the Web route now in the previous video I asked you to go ahead and try to see if you can put this into the controller called main and this is What I did so I create a controller called main controller and I move these controllers or these functions into the controller So the the get demo contact. Thank you and so forth. Now. This is a special case This is part of really of the get request that actually pull data from the database or in this case from the data File, but so it's kind of like a bit out of put out here because I don't want to put under the prefix of project Just just in any way how you do it But I'll leave it out here and it is actually inside your project controller as opposed to the main controller Okay, so here is my controllers the main controller here. It looks very similar to the other one I basically, you know name this function here and the operation is exactly the same. Okay, and then the project controller So you can see I leave this open add the portfolio here and just like before Okay, so now We are going to do the database part. So that means we're not going to use The data anymore. So from this data file here this file here. I'm not going to use this anymore to inject data You can leave this as is or you can remove it. That's fine. But when you can leave as is it's okay So now I'm going to Use the models inside the models folder here Right, we have a project model now this one here the way we created was that we you know did it Just locally here this time We're going to use the database to do this for us. So that means I'm not going to use any of these at all Okay, we're going to have the model class handle all the database connections And do the entire operations including retrieve update the lead and things like that. It has a built-in Framework already so you don't have to do that yourself if you want to do some Additional like queries then you can run your own But you see that most of these are already built into the system because it's so common Especially these quad operations. Okay, so what about what I need here is really a um A an attribute that will include these fields that will be injected into the model class Somewhere and then it will be used to that to pull data from the database Okay, so if you follow the one example and the user here so the user here as you can see it has a Attribute called feelable. This is the one that we need to include all your data fields Passed to that Model, okay, so we're not going to have the hidden fields and things like that here If you do you can put them here, but we're not going to use that just this one here Um, we will not use any token at all. So just the factory. That should be fine So go back here Then I'm going to just copy and paste it right in here and the fields that I need are really the id the name Description And then the url And that's it. That's all I need for this and now the model will require The database information in order to connect it to our database. So make sure that your environment File Is set to the one that we use so I'm going to use the my my sequel So I'm going to turn off the sql light So I turn this back on And we're going to use The database here Okay, so make sure everything is correct if it's not correct If you're my sequel my sequel server is not on then it's gonna have an error as well So let's save this and make sure your app is running. Okay now if you go to the site again and As you can see we are actually, you know, it's already like working because we Are trying to pass the fillable data to a the model already. So as you can see it has some error already Now also make sure that your database is working as you can see here This is the one we're creating yet the other time and it had some data already be populated If it's if you don't have any data, just make sure you run the migration again and run the cedar File to see this data with some information or you can add as manually doesn't matter. Okay, so Now let's go and fix our Information here since we're using the database now. This is not working anymore Right because we actually loading data from from memory only and this time we're trying to connect database Okay, so let's go back and see what we need to do in the code here now This part is done. So we can close this one In the project controller There's an error here I will see what that is, but we'll fix it Okay, so here is the project. We don't have that anymore. So I'm not going to use these anymore because Again, we let the the model class handle everything for us now All we need to do in here in this project controller is access the project model to load the data now if you go to the models file this a Inside the Is in here, I don't know where it is Where's the model at? Let's go over here Okay, and the models class in a look and model if you click on that one here, you will see that this model file It contains all the most of the things that you need. So here we go. We have the database connection, right? It does that for us it has the Function to fill this is the one. It's new to fill your database And there is the all for a get all of the data as you can see the query is A star it's like select a star from our column from your database table That's where it is and then there is an update as you can see update that will update or fail and there is a delete and destroy right with id and also Yeah others as well. Okay, so it's all in here and already included for us So therefore we don't have to do anything here other than just call those functions to use it. Okay, so the project file This is actually done This this is the model. We're just going to close this one here Now the controller is what we need to do some work Now when we try to load data from the portfolio when I go to this link What happens here remember in the old way was we actually called the session variable to load the data We passed a session Or this variable here to the view to render to the view, right? So this time we're not going to do that anymore Okay, we're going to turn this off And we're going to get the project still but this time we're going to get project From the project and then there is a function called all Okay, this all function will return all the data from the database Sets to a variable projects. We pass that to the view and Yeah, that's basically is right as you can see it's very easy now, right And um, so we got that on there the show form. This is just a show forms. We don't need to do that They delete with an id now. This one here is also very easy. We just need to provide The following so again, we're not going to use any of the sessions at all Okay, all this will be turned off. We just need to pass in the id to a delete function Right, so you would do that by calling We need to find the id so you can do something like this project There's a function called find Find Or fail and then you pass in the id to that function Okay, and that's it And once it's find it and then you need to delete it. So you would then set a variable here called project To that once it found that the id it returns the project or the id to this project And then we're going to call the function call delete It's going to delete that from the database Okay, and then we turn the user back to the portfolio and you can check this test of this project if it's true or false But for now, I'm not gonna I'm not gonna do that. Okay. So as you can see all of these here We moved us to do two lines Okay Very simple like that And then the show edit form Well, you know, let's do the delete off while while we're here. There is a delete although we did not We include in the button So I'm going to go up here later on we'll actually do it now. There's a delete and there's going to be another delete all It's kind of duplicate this Oh shift down And this one here has no id so just delete and we're going to call a function call delete all That's the action. All right. So let's say this and go back to our controller And let's create a delete. Oh, I'm going to copy this Every kind of similar and I'm going to delete this junk right here And if we don't need an id for this one, so just delete all and for this one Is a little bit different. I'm not going to find it right. I don't need to find it I just basically call it delete function and you do that There are a couple ways to do this. Um, you know, you can call the truncate function Using a different methods, but I'm going to use this quick function called Do the project The project is the one that we need queries Query and then it's going to call a function call delete That would delete your data. Okay, if you want to So by deleting the data if you're using a In auto increment free id Basically, the id's are retained where it was left off If you want to delete and reset that id all together Then you want to call the truncate function instead But that's all it is for delete all the data And that should be gone All right, so the edit form Um, let's see when we edit. Yeah, this is just to show the form, right? So again, we're passing the object Of a particular id of a particular Project so we need to do the search right once we search and we find it and then we can go ahead and then, you know Render that to the view so again, we're not going to need this anymore Okay, we'll do we'll let the model handle that for us. I need I study to get a project a single project And then I need to call a function call project find And then we pass them the id Once you find that id it's going to return the project object to the project We pass that to the view and render that to the view. Okay, just a single statement for this one here And then this is for the post right when the edit comes back We have the data and then with a particular id need to match that right so this time I'm going to make it a little bit different Because remember our fillable has the id included so that means I'm going to include that in the validation So just do something like this when I update this and include that in the id as we'll make it as required And then once it's all passed a requirement, right? So we're not going to do this again. We turn all these off All we need to do is now Before we turn back to the portfolio. We're going to Right, you need to update it. We're going to call the update function. Okay, so we still need to find it kind of like the delete with the earlier Yeah, like here, right? We need to find this project id first so I'm going to copy that put it right in here Find a project Okay, once I find a project then I will call the update function from this project object And I need to pass to this one here the Data coming from the request right the request member request contains all the information from the form fields Or you can pass the data as an array, but I'm going to call the request here And the request contains the data. So right into this function I'll call the request and then there's a function call all to pass all the records or all the fields And their values to the update function and it's going to update the project Based on this id here. We return here and once that's updated We return the user back to the portfolio page Okay, so again, it's a lot of code removed The add will be kind of similar. So I'm going to copy this again. The validation will be the same So I need to add id for both the edit and the add field Now for the add we did was that we actually randomize this here, right? So again, we're not going to need to send more Randomize the id so what I would do is I would generate the id as a random number from the view In the form first and then when it comes in here, we already have the random id here Okay, and it will validate that and then the same thing this time though. We're not going to search, right? We're just going to basically add the request all object to the The data source. So we're just going to basically do one very easy call object Great, what are we creating? We're going to get the data from the request again and get all the views That's it. That's updating. I mean adding new data to the database and then we directly use it back to the portfolio And we are done. So as you can see it's quite Short now, right? So we don't need all these stuff here. I will create this later. Maybe actually we can delete them now We don't need them anymore. Okay, we don't need that This we don't need this anymore the It's either And I don't need the sessions because you know data are now live from the database. So there's no need to do a session anymore Only do that for the the you know, the welcome message. So that's that And this one as well, we don't need this anymore. I'll keep the prefix as is But you can see that's not much much shorter Okay And these are our Crowd operations we call this api is although naturally an api you want to do it Um, it's a little different but the same idea you can also create api that will Be accessible via, you know a different app, right? Okay, so let's see what we can do and we just need to go and update the id for The two fields to um two views the add view and the edit view Okay, so someone here. I'm going to include The id view here. I want to copy this And just put a writing here And I'll make this Yeah, the name will be id Id I don't really need id here because I'm not going to do anything to it But the value is the project id and I also want to make the type to be hidden Aren't you show that to the user? So that's included already For the edit the add also need to do the same thing writing here This time though the value will be generated automatically. So we use the ran function here I'm generating anything from 1000 to 9999 This is my demo, of course, you can do it whatever you want And it will be included already when we run the submission form. Okay, so Um, I think that's it All right, so let's give it a try and see if Everything works if it's not we'll fix and see what the errors tells us So go to your site load the page make sure everything Okay, it broke Okay, so I have an error in the model Constructor the data file. Okay, so we have the data file here, right? Remember, we don't need that anymore and we need to basically just remove that completely because it's interfering So what it is is that in the file? The data file this one here remember is calling the Project model and our project model. We have we changed it, right? Right? So we already removed all these constructor information here. So therefore this is no longer irrelevant So therefore I'm going to um, you know, just turn off everything here or you can delete if you want to But since it's here it's going to run it So it's going to crash I want to turn this off And also from the web I need to turn off here up here Okay So actually we don't need this at all And um, yeah, that's completely moved because everything's now from the database Let's go and give it another shot Here we go Perfect. Let's go to the portfolio and boom, right? Perfect. Are these really data from a database? No, we can check it out. All right one thing. Let's go and edit here Let's say I'm going to edit my personal site. I'll call it exclamation mark and then exclamation mark here and then Leave that as is go and update Okay, now I'm going to view and refresh the page And boom, there it is Right, so we know that it's coming from the database How do we know for sure? You can do whatever we want, right? So I can I can edit this Right. So notice here we have the exclamation mark. I can be undoing this again Try to edit and see if it works. It seems to be working correctly Let's remove that remove everything back to what was before And update and boom. Oh, there's an error. All right, so Let's see Better data required doesn't exist. Oh, okay. I have an error there But we'll fix it But um, let's see. Yep. Let's fix the error in the required edit So when we do the edit part, let's close that data file The add will be find a controller ID is required A required or the D. Okay, maybe I did the wrong for the other one too Let's see required. Yeah, that's important All right, so one more time edit and remove that and this And hit that dot and then hit submit and Still an error. All right. Um, okay. So there's still an updated Add field is okay. Some errors in here for some reason. I probably missed Some information in this part yet Well, you know what? Um, it looks good to me. I couldn't find a reason why Let's see what the error says again and we'll probably shoot that a little bit So it says A column not found Projects updated at yeah, we don't have that project I don't think we have that In our database. I'm not sure why it's trying to call that. Um, so It says let's see you want to run migration again. Maybe let's let's do that again Let's see what happens. Okay, that could be it because I ran Different migration for the other one So let's let's go back and do your migration. Let's do this. Let's do a See how migration file I want to make sure it's still the way I had it under the Database if you remember in database migrations projects and This is the one so we have id name description. Yeah Okay, so let's do this php and php artisan Migrate and then reset. Okay, we do a reset and we roll everything back again We should not found projects table great projects table. Did I not do that right under the database migrations All right, so we reset the table and make sure that it's deleted from the database for us. See Well, okay, that's been deleted. Okay, that's good. Let's try one more time and now we're going to do it my great I'll use the same here, but I want to call it my great path That only and hit enter Okay, looks good and then now I'm going to seed it. So if you remember We do something like that and we should say db Code seed if the class is called project cedar Enter Have an error here. Let's see Project data is not defined constant. Okay, so it's some error, you know code might fix those first Now that we made some changes, let's go back to our cedars here. This is the one that we won't change Now it says there's an error, right? So I want to move this back to the local Let's do this Make it simple just seeding our database go here And I can't full constant in here. I'll just put a regular variable data and then put a dollar sign back down here And that should be good Let's go back and try again great Okay, so we are now done make sure the database is now fielded again with the data And it looks good. Okay. Let's give it a try and see what happens Let's go to the home page go to the portfolio. All right. Looks good. Let's do an edit And I put here the exclamation mark just one field And it's still not found. Hmm interesting something's going on here and we need to figure why All right, see what's going on. I The migration looks fine to me um I think it is it keeps calling a Field that we don't need When we run the If I remember correctly, let's see Let's see if that's fine. You know model Okay, so it it tends to um add the A field updated field if we don't need I think we just turn that off If I'm not mistaken, um, there is a variable The time stamp I see when we create I see a project here because it does require I think by default it does require a time stamp um And we can turn that off We don't need that in it to be in our program. So Uh, what did I put it and no project? Okay, so I think we just set a variable um call stamp We set that to false. I think that solves the problem We don't need time stamp variable If you want to then you will have to I think you have to add it as a part of the your database table If you don't need it, then we just turn that off Okay, that's I think that's what's causing us to see what happens Let's go back and try again Let's try this site again. All right. Let's try one more time Put exclamation mark and submit Oh, it's still not working correctly. Hmm. I'm stumped One more time Maybe it's not time stamps. That is a time stamps Time stamps. Maybe that's what it was Okay And submit come on There it is. All right. Okay. So that was it So just make sure you turn that off. Um, if you don't want to include it If you do need that if you don't include that then you have to add the column Time stamp will update it at and then also there's like two fields you need to add it in your database But just turn those off and then we're good to go Okay, so let's see if we can add a new project in here At one Best app and then here HTTP Best app And submit There we go. We added that here. Okay, perfect And we can also verify over here in our database just to make sure it's there And it's indeed here it is okay with id already filled in for us. We can also delete them All right, so it's good if you do another delete Just see that also these should be updated. Perfect. Now if you do a delete all And everything is now gone and our site will be completely wiped out as well All right, so perfect And uh, that's all we need to do in this example here So let's go back and just um Fill out the data back again, and we are now good to go. I'm going to go and seed it again so one more time And the data has been seeded. Let's go back and try again refresh our page and everything should come back There we go. All right, so That is how you would do it using the data model to connect to your database Make sure again that you turn this a variable off if you don't need to include that otherwise you need to include the updated Time stamping here and if you include a timestamp like we saw and the user here like this Not here, but we Need to turn that on off. Okay, so just do that part The controller is where all the action happens We need just to do the validation if you don't do that You just basically call these functions here and that will do the job. So review these functions A lot of these functions are also inside a model class if you want to look into that Where you can actually do more query if it's not what you're looking for Then you can do your operations here and you run your own query and um, you know run that and And then produce whatever you need now in a separate case where you actually create your own API You would not put your, you know Functions here. So instead of doing this way, you would use the apf file and put your api calls here And then, you know, this would be like the get put up Update delete request as opposed to here. We only use like the get and the post, right? So if you use an actual api Then you return the data back as jason data to where we're being called you put your source here And because using this type of apis you don't really need to view, right? So you just return a data back And then you will create a controller with similar to what you have here But instead of returning a view you will return the actual data back So you return the project back or whatever the message is if you're successful Maybe in a separate video or something we'll do something like that But for now, this is it for this model part two. So thank you and have any questions. Let me know