Author: Povilas Korop

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.

Friday, July 17th, 2015

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?

Thursday, July 16th, 2015

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().

Tuesday, July 14th, 2015

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?

Monday, July 13th, 2015

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.

Friday, July 10th, 2015

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?

Thursday, July 9th, 2015

CRUD: How to avoid building whole Form for Delete button

There is one annoying thing in Laravel RESTful approach – if you want to have Edit/Delete links in your tables, Edit is done easy with a link to URL, but for Delete you have to build the whole form. Is there a way to avoid it?

Wednesday, July 8th, 2015

There’s no PUT/PATCH/DELETE method, or how to build a Laravel form manually

Laravel RESTful controller mechanism is a very convenient thing – we generate Controller, write {{ Form::xxxx() }} methods in Blade templates and it works like magic. But there’s a little trick there which I want to talk about – it’s PUT/PATCH/DELETE methods for updating the database entries.

Tuesday, July 7th, 2015

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 =, 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.

Monday, July 6th, 2015