What made me go Yay! with Laravel 5.8

Yes, Yes, I know Laravel 6’s launch is around the corner and I am still talking about 5.8. But I wanted to go through the documentation before it is launched so that:
1. I don’t fall too far behind when Laravel 6 is available eventually.
2. I might be giving the Laravel Certification exam, so it will help there as well.

Just wanted to write about some of things that I had expected from the framework from time to time and found out that maybe I wasn’t the only one expecting such features. Came in as a good surprise.

1. Generated Columns

Now this is not a feature that I was waiting for, but it was good to know about generated columns

If you want to know more about the generated columns you can check here. The main advantage I see of using this feature would  mean avoiding the use of Laravel mutators.
Many apps use a mutator like

public function getFulltNameAttribute()
{
        return $this->first_name . ' ' . $this->last_name;
}

This is to get full name of a user by accessing it like $model->full_name with generated columns we can let MySQL handle it for us and we can create a column full_name as shown below

CREATE TABLE user (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    full_name varchar(101) GENERATED ALWAYS AS (CONCAT(first_name,' ',last_name)),
);

Now it might not be a great idea for everyone, but now at-least you have an option.

2. Enhanced Polymorphic support

Before 5.8 for any polymorphic relationships in your application you had to write custom queries, but now there are many inbuilt polymorphic relationship methods that you can use.
There are methods like whereHasMorph and whereDoesntHaveMorph
Read more about them in the docs and the API docs. This is such a time saver.


3. preserveKeys in API collection

This one made me really happy. If you have been using API resources in your project this would delight you as well

Now you can use the $preserveKeys property and your keys will not be rewritten by Laravel

class User extends JsonResource
{
    public $preserveKeys = true;
// other code //
}

In one of the projects, we were creating drop-downs based on APIs which returned an Array response. The arrays had a key:value pair, but Laravel would rewrite the keys and return a JSON object, thus making the drop-down values incorrect.
On top of it, the requirements were changed for different pages to have custom order for the drop-down items.

To fix it I had to change the entire code base and make sure the API returned a response as an array of objects.

[
 {
   id: 1
   name: 'abc'
 }
]


Although, this new approach was much better than the earlier one for a couple of reasons:
a. When you return an object from your API, and use that object on the front-end, the order of the items will still not be guaranteed, as JS doesn’t guarantee ordering of object
b. Using an array in response, made the order of the response to be preserved and it also allowed for having more control over it on the front-end using a library like lodash

But still in some places I could have kept the old API response where ordering was not required. Sadly this option was not available at that time.

This all for now, but as I am still going through the docs I might see more of such changes and update this article.
All in all having these changes solidifies my belief that Laravel is one of the best frameworks around(not just in the PHP world, I would say, but overall 🙂 )

Leave a Reply

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