Android

Quick Overview

Wordpress

In the previous Laravel Tutorial we have discovered the Laravel routing and views and display tags submission form.

If you are new to this tutorials will recommend to please first go through this tutorials and then you can continue with this tutorial.

Without wasting much lets continue with submitting the form.

Step 5: Submitting the Form

With the form in place, we are ready to handle the POST data and validate data. Back in the routes/web.php file, create another route for the POST request:

This route is a little more complicated than the others.

First, we are injecting the Illuminate\Http\Request object, which holds the POST data and other data about the request.

Next, we use the request’s validate() method to validate the form data. The validate method was introduced in Laravel 5.5 and is a nice shortcut over other methods used for validation. As a bonus, the validated fields are returned to the $data variable, and we can use them to populate our model.

We require title, and using the pipe character; we can define multiple rules. All rules can have a max of 255 characters.

If validation fails, an exception is thrown, and the route returns the user with the original input data and validation errors.

Next, we use the tap() helper function to create a new Tag model instance and then save it. Using tap allows us to call save() and still return the model instance after the save.

Typically, you would have to do the following without tap, it just adds a little syntactic sugar:

If we want to populate a new model with data, we need to allow the fields to be “fillable” via mass assignment. The fillable property is designed to prevent fields from being mass-assigned except for the items you define in the array.

In our case, we are validating each field so allowing them to be mass-assigned is safe. To allow our model assign values to these fields, open the app/Tag.php file and update it to look like the following:

If we wanted to prevent mass-assignment, this is how our code would look:

The last thing we do in our POST route redirects the user back to the home page after saving the tag successfully.

Step 6: Testing the Form Submission (Optional)

Laravel makes HTTP testing a breeze for performing integration tests against routes and middleware, so let’s write a few feature tests to verify our code works as expected.

Before we get started, we need to adjust a few things in our phpunit.xml file so that we can use an in-memory SQLite database. You will need to make sure that you have the proper PHP modules installed.

Using environment variables, we can change the database connection by adding a few new variables to the config:

Next, remove the placeholder test that ships with Laravel:

We are ready to start testing the /submit form through HTTP requests to make sure that the route validation, saving, and redirecting are working as expected.

First, let’s create a new feature test to test against our route:

The command creates a new testing file with the proper dependencies, including a RefreshDatabase trait that we are going to use to verify that our links are being saved to the database when valid.

Open the new tests/Feature/SubmitTagsTest.php file and let’s define a few skeleton tests in the body of the class that we are going to flesh out:

These tests should give you a high-level overview of what we are going to test:

  1. Verify that valid tags get saved in the database
  2. When validation fails, tags are not in the database
  3. Validation should fail when the fields are longer than the max:255 validation rule
  4. Validation should succeed when the fields are long enough according to max:255.

We might be missing some things, but for your first Laravel application, this is a decent list that should illustrate some basic HTTP testing techniques in Laravel.

Conclusion

Congratulations on making it through the tutorial. This guide was designed to get you started on building your app, and you can use this as a building block to gain the skills you need to develop your application.

I hope this introduction to Laravel shows you why so many people are excited about the framework.

Happy Codding!!!

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here