Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

butifarra's avatar

How to search for a mutated attribute?

Hi to all! I have this mutated attributes. It is saved as 1 or 2 in the database, but cast as Dólares or Pesos. Well, I need to use Eloquent "where" queries to search "moneda" for "Dólares" or "Pesos". How is it done, if possible at all? (Actually, the need I have is with other table much more complex, but the concept is exactly the same) Thank you in advance.

protected function moneda(): Attribute { return Attribute::make( get: fn ($value) => ($value === 1) ? 'Dólares' : 'Pesos', set: fn (string $value) => ($value === 'Pesos') ? 2 : 1, ); }

0 likes
2 replies
kevinbui's avatar
kevinbui
Best Answer
Level 41

That does not sound hard. I don't know what those words mean, but hopefully this makes sense.

class YourController extends Controller
{
    public function index()
    {
        $results = YourModel::where('moneda', request('moneda') == 'Dólares' ? 1 : 2)
            ->get();
    }
}

Please or to participate in this conversation.