At the end of this tutorial, you’ll be able to execute CRUD (Create, Retrieve, Update Delete) in Laravel 5.7. You’ll also learn how to add pagination in displaying of records.
Open your terminal and download the Laravel Framework by executing the command.
composer create-project --prefer-dist laravel/laravel school "5.7.*"
We specified the version of laravel to 5.7 for this tutorial.
It takes a few minutes to download. Now, go into the school directory.
After that, start your server.
Make sure to change according to your host and database settings.
3. Model and migration in one command.
If you want to create model and migration one by one, you can also do it. In this tutorial, I just want to let you know how to do in one command in order to make it faster.
4. Let’s create table schema for students.
After filling out the field data, execute the migration.
After migrating, the students table will be added to the database together with migrations, password_resets and users table.
Go to app» Student.php and add fields as fillable.
In creating of a controller, it is recommended to follow proper convention like, making the first letter to be uppercase and putting “Controller” at the end.
Go to routes >> web.php and add our controller in our routes.
Let us add the main layout of our resources >> views >> layouts >> main.blade.php.
Open StudentController and add the code below.
Next, we need to add a form inside create.blade.php.
After that, we have to create a script the will validate the data upon submission and insert it into the database.
and go to http://localhost:8000/students/create . Try to insert dummy data and should have successfully added a new one.
After we’re able to insert data into the database, we can now display it in our page.
Next, let’s display the data in our view. Now create file resources >> views >> students >> index.blade.php and copy the code.
Notice that I set the pagination already and limit to 2 per page. You can change this according to what you need to be displayed.
Let’s test the code again. Now, start your artisan and go to http://localhost:8000/students .
Let’s modify our index.blade.php and add additional column into table and put EDIT and DELETE buttons, respectively.
Lastly, start your artisan and test it again. If you notice, I didn’t add validation. You can add validation like how I added in create method.
This is the last operation we need to execute. It’s simpler because we already added delete button above. What we need to do is to add the script below inside your StudentController.
Thank you for reading this guide. I hope you’re able to follow through all of the things above and able to execute CRUD in your local server.
If you have any questions or suggestions, please feel free to comment below.