Support the ongoing development of Laravel.io →
Database Eloquent

Hi,

It's my first post and, in fact, isn't properly a question, but a request for suggestions because my problem, for now, it's solved. I hope there's a better and elegant solution from my issue.

The majority of tutorials for begginers about Laravel on Internet explain how to make a new solution, using the Eloquent patterns. But it's hard to find tutorials and articles talking about migrations from legacy databases and systems to Laravel's architecture.

I use MySQL and the columns' names are sometime in uppercase sometime in lowercase. And when I try to show the data on my View, I have to guess that.

    @foreach($users as $user)
        <p>Name: {{ $user->name }}</p>
    @endforeach

The code above returns nothing, but the code bellow shows the user's names:

    @foreach($users as $user)
        <p>Name: {{ $user->NAME}}</p>
    @endforeach

To solve my issue, I have to do that on my Model:

    public function __get($key)
    {

        if (is_null($this->getAttribute($key))) {
            return $this->getAttribute(strtoupper($key));
        } else {
            return $this->getAttribute($key);
        }

    }

Is it a god solution? How can I have a better one?

Thanks in advance the help!

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

carloshc carloshc Joined 4 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.