Laravel 4: How to use an Accessor in a where-clause

accessor, database, eloquent, laravel-4, php

Solution

Yes, you can use it to filter query results:

User
    ::where('gender','=','male')
    ->get()
    ->filter(function($item) {
        return $item->specialName === 'Luke';
    });

(!) Note that filtering will be applied after quering DB, so in case of big data this solution will have performance issues.

For more details I've googled for you this Collections tutorial.

Also I suggest query scopes may be useful to complete your task in best way.

Problem

Is it possible to use an Accessor for comparison in Laravel 4, for example: ``` class User extends Eloquent { // define Accessor public function getSpecialNameAttribute() { return 'Joda'; } } $User = User::where('gender','=','male')->where('specialName','=','Joda'); ``` ?

Original source