Topic 8
Topic 8
CRUD Operations
Introduction
CRUD (create, read, update, delete) is an acronym for the ways in which stored information
can be operated on. It is a term for the four functions of persistent storage. CRUD usually refers
to operations performed on a database or data store but can be applied to higher level functions
such as soft deletes, where information is not necessarily deleted, but marked as deleted
through a status (MDN, 2022).
During this topic you will explore the ways in which Laravel can work with these operations,
but before you begin, you will start a new Laravel application, set up the database, and explore
a problem in which you will model and create its models, controllers, and migrations.
Laravel activity
Explanation
Read
We will interpret that, to read, we will get two views: index.blade.php and show.blade.php,
since index lists all the records of the students table, and show presents a particular record. Let's
start with index:
As you can see, in a variable called "students", we make use of the student model and fetch all
its records. Finally, we return the index view of the student folder (Laravel interprets directories
and files with a dot, so student.index is equivalent to student/index.blade.php. It is not
necessary to put "blade.php", when returning a view, Laravel knows that it is a file of Blade
type).
If you followed all the steps up to this point and you have your services started, you should see
the following when accessing the URL http://crud.test/student.
For the show view, you will notice that when listing the elements of the table, thanks to a
@foreatch, the object that was returned when displaying the index view (students in plural), we
create a Table Row ("<tr></tr>") and in each <a> tag in the href property we send it to the path
"student.show" with the parameter of the ID that corresponds to each student (in singular):
Notice that we look up a student by its ID and store it in a student variable (singular), which we
inject into the returning view.
Delete
Although we don't have records to delete yet, we could directly inject some records manually
into the database, but we will work on its function in the controller:
Notice that we search by ID a student and store it in a variable "student", then, thanks to the
delete () method, we delete it and return with a redirect to the path /student with a message.
Notice in the index view file that by clicking on the "Delete" button, using a form, we are
sending the student's ID to the path "student.destroy":
8.2. Create
Within the create () function, we must add the following in our controller:
The only thing we do is to return the create view, which is a form that, when filled, will be
processed by the store () function. Inside the store () function we will put the following:
Validations can be done by the frontend (placing the required attribute in the input text of each
field) or by the backend through the model or the controller. In our case, we will do it through
the controller with the validate function that comes incorporated in the Request.
We store the object in a variable (which we will later use to create the record), where each
attribute will be validated. Remember to see the official documentation to determine the types
of constraints and the general use of validations in https://laravel.com/docs/7.x/validation).
If the validation passes because of the model, you will be able to create the record in the table,
passing it as parameter, the variable where we store the validated object; otherwise, it will
return to the view with content in an internal variable called "errors". Observe the following in
the create view:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br />
@endif
If there is an error of any kind, we will display an alert with html and Bootstrap, and we will
walk through the errors via a @foreatch.
Very important. As we use the model to process the information directly in the table,
Laravel protects all the Eloquent models, so we must specify the attributes that will be able
to be treated in a massive way. We do this with the protected function fillable, which,
inside of it, we will put the attributes that will be treated from our views, and validated by
the controller:
Once the fillables are declared in the model, the store () function can work, redirecting to
the index view.
8.3. Update
Finally, after listing all the records, viewing a particular record, creating a new one and then
deleting it, it is time to update an existing record. As you can see, in the index, there is a Table
Row containing a <a> tag that sends to a path called "student.edit" and sends the student ID as a
parameter:
The function in charge of processing this request is edit () and its content is the following:
public function edit($id)
{
$student = Student::findOrFail($id);
The function searches for the ID and returns the student.edit view with the variable where the
search result was stored.
The view displays the information in the inputs in its value property, showing the following:
When saving, the function in charge of processing the information update, the update ()
receives the ID of the student and the Request with all the information of the inputs.
A good trick to see what the request has is to use the "dd" command:
This will display information in an array with a variable content according to each form
without executing the immediate lines that come in the controller. It is, in short, an alternative
method of depuration:
Now that we know what the Request has, we proceed to process that information in the
controller:
As in the store () function, we validate all the information and, if it passes, we look for the ID
to the student, and through the update () method, we inject the validation parameter where all
the information to be updated comes. We redirect to the URL /student with information.
Conclusion
In general, CRUD operations are the basis for information processing in most business
applications that require record additions, deletions, or modifications.
In the same way that we make a CRUD through the model, views, and controller in Laravel
from the controller and API routes, we can create CRUD to be used by third party applications.
This is very useful when our application will be used by other applications, for example, mobile
applications, where the backend and frontend are independent projects. The principle is the
same, only the return of the information changes; instead of returning a view, a JSON object is
returned.
Checkpoint
References
MDN. (2022). CRUD. Retrieved from
https://developer.mozilla.org/es/docs/Glossary/CRUD
Additional Resources
The following links do not belong to Tecmilenio University, when accessing to them, you
must accept their terms and conditions.
Videos
Readings
Activity 8
Description
Objective
Requirements
Computer with Internet access, have XAMPP, or the WAMP, MAMP, LAMP or Laragon Stack
installed.
Instructions
You will create a system for data additions, deletions, changes, and deletions that contains the
following features:
Deliverable(s)
A report document that includes all the screenshots of all the views of your project and the
URL of your project on GitHub.
Evaluation criteria:
Homework 7
Instructions
Create a personal system where you can enter the grades of your subjects. Therefore, do the
following:
1. First, create a Laravel 7 project called "grades", just like the project on GitHub.
2. Then, develop a CRUD where you list the subjects.
3. When selecting a subject, the grades for each gradable activity (homework, activity,
quick test, midterm, among others.) should be displayed.
4. Next, you must have the possibility to add a new evaluable activity, where you will
put the type of activity, grade, date and, optionally, a space for a grade.
5. Finally, the system must allow to modify activities and grades, as well as to delete
them.
Deliverable(s)