Tips for Developers New to Laravel Framework – Part 2

laravel framework tips for beginners

Introduction

Laravel is a powerful open-source MVC framework for PHP to build web applications. The Laravel framework (which is built with Symphony) was first released in February 2012 by Taylor Otwell, who is still maintaining it till this day. Laravel helps developers not “reinvent the wheel” by including commonly used tasks like authentication, routing, database interaction, and more.

In the previous Laravel article, we covered the basics of the Model–View–Controller (MVC) framework which Laravel is empowered with. We went through the Laravel lifecycle, and learned about Laravel’s Eloquent ORM namespacing.

In this article, we’ll cover the route model binding in Laravel framework, how to change and customize authentication user details, and how to fake PATCH and DELETE requests on form submission.

Route Model Binding

Laravel offers an easy and convenient way to inject model IDs to specific routes and retrieve the corresponding model instance that matches the given model ID.

Implicit Binding

Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable name matches a route segment name. For example:

Using {item} wildcardSince the $item variable is type-hinted now as the App\Item Eloquent model and the variable name matches the {item} wildcard URI section. Laravel will automatically inject the model instance that has the matching ID (Default but can be overridden) from the request URI. If matching the model instance failed, a 404 HTTP response will be automatically generated.

Overriding the key name

In order to bind the model instance to a column other than ID which is set by default as mentioned above then you refer back to your model class which in our case it is located in App/Item.php

Overriding the model key name

Customizing Laravel Auth

Laravel provides a quick and easy way to create authentication for your app it is a simple as typing: $ PHP artisan make: auth in your terminal which will allow Laravel to create all the appropriate routes, controllers, models and migrations needed; however, most of the time you need to customize it by adding or removing some fields or tweak existing ones here we will go through the process of adding a phone number to our user. First Let’s start a migration.
$ PHP artisan make:migration –table=users add_phone_to_users
We added the option –table=users to update our existing table “users”. Navigate to our database/migrations directory and add your new column.

Customizing Laravel Auth

And finally, run your migration.
$ PHP artisan migrate
Now that we have added a phone column to our database all we need to do is add our new column to the fillable array (which allows for mass assignment) in the model class.

Customizing Laravel Auth

Finally, add that that column to our RegisterController in the validator and create methods and in the register view form resources/views/auth/register.blade.php

Customizing Laravel Auth
View form:

Customizing Laravel Auth

Faking PATCH and DELETE Requests

Following the RESTful approach, you will need to send PATCH and DELETE Requests which is not supported in browsers so using the HTML attribute of method=”PATCH” will not be interpreted by the browser and it will be set back to the default GET method so a good workaround this that Laravel provides is making the form method to be set as POST and in the very first line after the form tag, we add the helper function method_field(‘PATCH’) which add a hidden field that will be interpreted by Laravel as a PATCH request and will route accordingly.

Faking PATCH and DELETE Requests

Finally, the output of method_field(‘PATCH’) will be finally rendered on the browser will be something like this. (Same works for DELETE Requests)

Conclusion

This article was designed to take you a little bit further with web development using the popular PHP web development framework. In this tutorial, we’ve gone through:

  • Route model binding
  • Customizing Laravel authentication
  • How to fake PATCH and DELETE requests

In the next Laravel article, we’ll cover the all naming conventions for routes, Eloquent, and model names.

Written by Mohamed Atallah
Copyedited by Mohamad Hussein

Comments
Comments are closed.