Quick Tips


Why use $appends with Accessors in Eloquent?

Eloquent has a convenient feature called Accessors - you can define your own custom fields on top of existing in the database table. But then there's an Eloquent property $appends - should we use it or not? And what's the difference?

belongsTo() and withTrashed() – linking to deleted row

Let's say we have DB table products, which is linked to table categories with a field products.category_id = categories.id, Eloquent helps us to define a relation easily. But what if the category gets (soft) deleted, but we still need to have that relationship to be returned for history reasons? You can use withTrashed() method here.

Did you know: affected rows after Eloquent update

Another quick tip - if you need to know, how many rows were actually affected if you launch Eloquent update() statement - it cannot be easier.

Laravel 5.1: empty option in Form::select with lists()

There is one small upgrade in Laravel 5.1 which breaks apps with earlier versions - it is related to <select> dropdown items. For a long time, Eloquent had a useful way of passing options to Form::select() - but adding an empty parameter doesn't work in Laravel 5.1 anymore. Here's what to do.

Class ‘Form’ not found in Laravel 5?

One of the biggest confusions for people switching from Laravel 4 to Laravel 5 was using the Form class. Basically, Form::open() and related stuff...

How to customize “artisan down” page?

Artisan has a convenient command artisan down for an occasion when you need to put your web-project down for a while - for the case of deployment, maintenance or anything like that. Question - how to customize that temporary error page?

Don’t forget the fillables!

Just a quick tip. I've noticed a really typical mistake by developers, which caused some headache for myself and for others - you add a new field into the database table, but for some reason it isn't automatically saved.

AND-OR-AND + brackets with Eloquent

Eloquent is a great thing - you can build your query step-by-step and then call get() method. But sometimes it gets a little tricky for more complicated queries - for example, if you have multiple AND-OR conditions and you want to put brackets, how to do it properly?

How to configure PHPStorm for PSR-2

Just a quick tip - since Laravel 5.1 now is coming with PSR-2 support (most important change is spaces indent instead of tabs), it makes sense to configure your editor to support that by default, if you haven't done so already.

How to deal with possible empty Input variable?

Laravel is pretty strict about any kind of errors - if you try to use undefined variable or don't pass a necessary parameter you immediately see Whoops or another kind of error, depending how you handle them. There's a little trick with Input variables that might save you some time and lines of code.