Quick Tips

View logs with Artisan Tail command in Laravel 5+

If you moved from Laravel 4 to version 5 or 5.x - you will miss an Artisan command artisan tail for your log viewing. How to get it back? Luckily, there's a package for that.

Eloquent: incrementing columns without update() function

Eloquent mechanism isn't limited to just create/update/delete functions - that's why it's awesome. One of those helpers come to rescue when you need to increment a column, basically run update X set Y=Y+1 where id = Z - apparently, there's no need to run update() function for that.

Eloquent: How to make lists() work for Accessor fields?

A very convenient way to populate dropdown options with Eloquent is to use lists() function. But it doesn't work out of the box with "Append" fields, also called "Accessors". Here's a small trick to make it work.

Schema Builder: changing table columns (only Laravel 5.0+)

While working with database migrations and schema builder, sometimes we do need to make changes to the columns that already exist. Can you do that and how?

Eloquent: filter only rows which has related “children” rows

Simple use-case: you want to filter only those categories which have at least one product. Or course, you write Category::with('products')->... but how do you filter out those empty categories? Those with no product? There's an app function for that: has().

How to customize error messages in Request Validation?

Laravel 5 has an awesome new function - Request Validation. It separates the logic of Validation into kind of a separate layer - Requests, which reside in the folder app/Http/Requests. After the form validation, it auto-magically shows error messages. But what if we want to customize them?

Did you know about view()->exists() function?

When you load a View from the Controller, there could be a situation when you don't actually know which view to load - the name could be dynamic. For example, you would use different Blade templates for sending emails. There is a neat function to assure your chosen View file actually exists.

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.