Following on from my post on creating the pain form, today I need to work on the back-end logic that will capture and utilise the pain data. Eventually this data will be presented in a format that is useful for a user, and potentially a health practitioner or manager.
The first aspect of this back-end logic is utilising Laravel’s Models, Migrations and Controllers.
The Model
Laravel ships with a user model, controller and other authorisation classes. I may use these, or integrate them into today’s problem eventually. But for now, my objective here is to create a model for capturing the data and using elsewhere in the app; data security concerns are not relevant to me right now (famous last words).
A Laravel model is a class that allows us to access and work with the database intuitively. If a model is created in the command line with a -m option, a migration file is also created, where the table can be defined in php and then created or dropped as required.
For this exercise, I’ll be creating a PainJournal model.
The Migration
The migration file contains the different aspects of the database table that will be built to satisfy the pain journal model. For now, I will add a string description field (use string in place of a varchar) and an int pain field. The can be updated and rolled back to add functionality at a later date
Creating the PainJournal model with the -m migration flag also created a migration/db.table file associated with it. The default name of the table is called pain_journals, which has auto-increment ids and timestamps, and to which I will add the fields the be captured by the pain journal form.
The Controller
The controller is where form requests will be received and acted on, so the route page will need to be updated to point towards this new controller.
- For form controllers, we must add the Request namespace (use Illuminate\Http\Request)
- Also, we must add the name space of the model that is being used/saved to (use App\PainJournal)
- Laravel has a built-in hash security function (e.g. $password = bcrypt($request[‘password’])), but this does not seem like an immediate answer to the data security needed for this app, as what is posted to the database is a hash, and AFAIK, there is no way to get the original message back apart from brute force – generating random values and hashing them to get the correct hash – so this is not the function I need for protecting personal data from programmers.
There is a convenient way of creating this controller with CRUD methods already defined. php artisan make:controller [ControllerName] –resource will create a controller with methods to list all, create, update, delete, etc.
- With all these methods defined in our controller, in the web.php file which manages routes, a single line which points the object to be CRUD’ed to the controller will handle all such requests
- I.e.: Route::resource(‘pain_journals’, ‘PainJournalsController’);
- If we look at the routes that are listed for this app, we can see that the above command has pointed CRUD requests to the appropriate controller
The next step is to define within the Controller methods what they should do.
Putting it All Together
The first CRUD functionality will be the simplest; displaying all the user’s pain journal entries. The controller has a predefined index() method that will perform this function.
- The first step is importing the PainJournal namespace of the PainJournal model to the controller
-
Then define a variable will which hold an array of all instances of the fetched PainJournal model:
- $pain_journals = PainJournal::all();
- Then we will return a view which will display this information, and pass it the array. In this instance, I’m passing a page called ‘myhealth.blade.php’
-
Finally, we create a view that will accept the array of entries and loop through and display them in a table:
And this is the output:
This is the template for data retrieval. Next, data creation.
Data Entry/Creation
After some unnecessary hiccups, it turns out that data entry with Laravel can be very simple and easy. There is more to the system that I need to learn over time, but the basics with this simple controller setup are working well enough for this project.
The basic steps are:
-
Set the action of the form to the name given for the route::resource in the web.php route document
- Route::resource(‘pain_journals’, ‘PainJournalsController’);
- In the create() function of the controller, provide the view which has the form
- In the store() function of the controller, set variables to retrieve the posted values from form submission
- Create a new model object and set the values of the model object to the form values
- Save the model object
-
Redirect to whatever.
- In this application, I want the user to be redirected to the health suggestions after they’ve made an entry.
- There should be entry feedback sent to this redirect, but for now this functionality will be enough
Fig. The form page with the bodymap
Fig. The successfully captured and output data.
Ethical Aside
It occurs to me that I haven’t thought through data security concerns re: database and health information; sensitive health information is right out in the open and visible to the data admin (me). There must be some standard methods as there are for protecting passwords, but I’ve not researched them yet. This will either need to be a step I take soon, or a deficiency that is catalogued and discussed further.