Using attributes to change table column names
Sometimes we may be working with a database that was created using less-than-logical column names. In those cases, we can use Laravel's Eloquent ORM to allows us to interact with the table using more standardized names, without having to make database changes.
Getting ready
For this recipe, we need a standard Laravel installation, a properly configured MySQL database, and our migrations table set up by running the command php artisan migrate:install
.
How to do it...
To complete this recipe, follow these steps:
Create a migration for our table with the column name
odd
, in the command prompt:php artisan migrate:make create_odd_table --table=odd --create
Create a migration to add some data to the table, in the command prompt:
php artisan migrate:make add_data_to_odd_table
In the
app/database/migrations
folder, open thecreate_odd_table
migration and add the schema:use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration...