Quick Overview

In the previous Laravel Tutorial we have discovered the Development planning , installation of laravel and building a list of tags.

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

Without wasting much lets continue with laravel tinker and routing.

Step 2: Building a List of Tags (Continue)

Using the tinker you can start playing around with the model data:

We have the data place and a model to interact with the database. We are now ready to start building the UI to add new tags to the application.

Step 3: Routing and Views

To build out a view showing the list of tags, we need to update the main project route and also define a new route that will display our submission form. We can add new routes to our application in the routes/web.php file.

In the web routes file you should see the default route below:

To create a new route we can either use a route closure or a dedicated controller class. In this tutorial, we will use closures for our submission and index routes.

First, let’s update the home route by getting a collection of links from the database and passing them to the view:

The second argument can be an associative array of data, and the key ends up being the variable name in the template file.

You can also use a fluent API to define variables if you prefer:

Next, edit the welcome.blade.php file and add a simple foreach to show all the tags:

Here’s what the welcome.blade.php HTML should look like:

If you refresh your browser, you should now see the list of all the tags added. With that all set, let’s move to submitting tags.

Step 4: Displaying the tag Submission Form

We are almost done creating our first application in Laravel. We will round out this Laravel tutorial with the ability for others to submit tags into the app, which requires : title.

Since we’ve added all the core structure, model factory, migration, and model, in the last section, we can reap the benefits by reusing all those for this section.

First, create a new route in the routes/web.php file:

Next, we need to create the submit.blade.php template at resources/views/submit.blade.php with the following boilerplate bootstrap markup:

There’s quite a bit going on in this form, so let’s go over the major points that might be confusing when you are new to Laravel.

Near the top of the form, we have a blade conditional that checks to see if there are any validation errors. When errors exist, the bootstrap alert message will be shown, prompting the user to fix the invalid form fields:

Each individual form field checks for validation errors and displays an error message and outputs a has-error class:

If the user submits invalid data, the route will store validation in the session and redirect the user back to the form. The {{ old('title') }} function will populate the originally submitted data. If a user forgot to submit one of the fields, the other fields that have data would be populated after validation fails and errors are shown.

If a field has an error, the first() method returns the first error for a given field:

That’s it for this part we can see more on next post till then play with it.

In the next part will see Submitting the form and Testing the form submission and much more.

Happy Codding!!!

What Others Are Reading

2 thoughts on “Laravel Tutorial : Building Your First Laravel Application – Part 2”

Leave a Reply

Your email address will not be published. Required fields are marked *