Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do Laravel Eloquent Model Attributes map to a table?
Laravel Eloquent is an object-relational mapper (ORM) that provides an elegant way to interact with databases. Each table in your database corresponds to an Eloquent model that handles all operations on that table.
Model and Table Mapping Convention
Laravel follows a specific naming convention for mapping models to tables. If you have a table called users, the model should be named User (singular). Similarly, customers table maps to Customer model. This plural table singular model pattern is Laravel's default convention, though you can customize it as needed.
Database Configuration
Before working with models, ensure your database connection is properly configured in config/database.php and .env file.
Sample .env configuration for MySQL ?
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=test DB_USERNAME=root DB_PASSWORD=
These environment variables are referenced in config/database.php ?
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
],
Creating a Model and Controller
Create a User model using Artisan command ?
php artisan make:model User
This generates app/Models/User.php ?
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
}
Create a controller to work with the User model ?
php artisan make:controller UserController
Method 1: Using getAttributes() with Data
When your table has data, use getAttributes() to retrieve all model attributes mapped to table columns ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function index()
{
$user = User::find(1);
print_r($user->getAttributes());
}
}
Array(
[id] => 1
[name] => John Doe
[email] => john@example.com
[email_verified_at] =>
[password] => hashed_password
[remember_token] =>
[created_at] => 2024-01-01 10:00:00
[updated_at] => 2024-01-01 10:00:00
)
Method 2: Getting Column Names Only
To retrieve just the table column names without data, use this approach ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function index()
{
$user = User::first();
$table_columns = array_keys(json_decode($user, true));
print_r($table_columns);
}
}
Array(
[0] => id
[1] => name
[2] => email
[3] => email_verified_at
[4] => created_at
[5] => updated_at
)
Conclusion
Laravel Eloquent automatically maps model attributes to database table columns using naming conventions. Use getAttributes() to access attribute-value pairs or convert the model to JSON and extract keys to get column names only.
