How to Get Columns Names from Model in Laravel

Hello Geek, This example shows how to get the names of columns from a model in Laravel. I’d like to show you how to get column names from a model in Laravel. This tutorial will show you how to get column names from a query in Laravel. I’ll show you how to use Laravel to get all fields from a model.

In Laravel, you can get the column names of a table associated with a model by using the ‘getFillable ‘method or the ‘getTable ‘method combined with the ‘getConnection ‘method.

First, let’s consider the ‘getFillable ‘method. The ‘getFillable ‘method returns an array of column names that are allowed to be mass assigned. Mass assignment refers to the assignment of multiple attributes to an object in a single line of code. By default, all columns are guarded from mass assignment. To specify which columns are fillable, you can define a fillable property in your model class.

Here’s an example of a model class with a fillable property:

This example works with Laravel 6, Laravel 7, Laravel 8, and Laravel 9.

aboutthenerd.com How to Get Columns Names from Model in Laravel

How to Get Columns Names from Model in Laravel

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
}

To get the fillable columns of a model, you can simply call the ‘getFillable ‘method on an instance of the model:

$user = new User;
$columns = $user->getFillable();

print_r($columns);

The output would be:

Array
(
    [0] => name
    [1] => email
    [2] => password
)

If you want to get all columns of a table, regardless of whether they are fillable or not, you can use the ‘getTable ‘method combined with the ‘getConnection ‘method. The ‘getTable ‘method returns the name of the table associated with a model, and the ‘getConnection ‘method returns an instance of the ‘Illuminate\Database\Connection‘ class, which you can use to execute raw SQL queries.

Here’s an example of how to get all columns of a table:

$user = new User;
$table = $user->getTable();
$connection = $user->getConnection();
$columns = $connection->getSchemaBuilder()->getColumnListing($table);

print_r($columns);

The output would be:

Array
(
    [0] => id
    [1] => name
    [2] => email
    [3] => password
    [4] => remember_token
    [5] => created_at
    [6] => updated_at
)

Note that the ‘getSchemaBuilder ‘method returns an instance of the ‘Illuminate\Database\Schema\Builder‘ class, which provides a convenient API for creating and modifying tables.

In conclusion, in Laravel, you can get the column names of a table associated with a model by using the ‘getFillable ‘method or the ‘getTable ‘method combined with the ‘getConnection‘ method. The ‘getFillable ‘method returns an array of column names that are allowed to be mass assigned, while the ‘getTable ‘method combined with the ‘getConnection‘ method returns an array of all columns in a table.

All the best nerd!

Leave a Reply