Model View Controller

Hello World! :)

I'm making this website mainly for my self to memorize better/quicker steps of making the functionality of the Laravel. On this page, I'll be using Laravel 6 which is LTS (Long Time Support).

Our objection is to have CRUD pages which can:

1. Create content.
2. Read content.
3. Update content.
4. Delete content.

As Laravel is using MVC Model, each time we have to create a model, controller, and view pages.

I've made several projects in Laravel by now and only one fully functional which should be used by Broxburn Bottlers. Here will do similar, but will be more advanced so no point to describe even that one.

I'm always looking for improvement when possible and here I will use this method as well.

While creating a model, I can create as well Migrations for the DB by adding only -m, here an example:


Now, while having Post Model with Migration, I went to the Migration file and edit it to my needs and used after that command 'php artisan migrate' to send it to DB:



While creating a controller, I'll add all the resources in one command:


This way, instead of making Model, then Migration, then Controller, then all resources by hand in a Controller for Create, Read, Update & Delete, I've done it much quicker. So, when you do a production for a customer, it's worth understanding it and in which order you should do it. I like to use Google Forms to have steps written and when doing one step I can tick it off. You'll do once the template and you can reuse it all the time. Here is an example of Google Form for Post with more features than just our MVC:




If/when you do a small app, it won't matter if you'll use it or not to plan each step. But, when you do a bigger project Preparation by planning is THE KEY!

Now, there's one more thing to remember. While creating a Model we should not use a plural name, we should use a singular. Why is it so?

Let say we have a name for and Model and Controller post.php, we can easily mix up stuff. When you start doing projects in Laravel, or even other MVC frameworks, it's better to always use singular name Post.php let say and for controllers plural. Yes, it would be posts.php. Here is an example of a controller with empty resources just after creating it with a name Posts.php, I prefer to use PostsController.php if so:


Hopefully, this will give you some ideas for the future. To make it work as a fully functional CRUD, now what we need is add functions in reach resource in our controller. Next Add Routes to map functionality on our website. And, create View pages to display the content.

Now, please check what is missing in the Google Forms :). One thing is missing and after reading this part, you should know what I did not put and should be added in order to work after creating all the steps from Google Form. Can you see it?









No comments:

Post a Comment