Laravel get all columns from table example

Laravel 6 , Laravel

Nicesnippets

14189

05-12-2019


Hi dev,

In this example,i will get all columns of a table in laravel 6 . we are pick up all columns of model in laravel 6. it can get all column names of a table in php laravel 6.

So sometime you may need to get table name from controller into your laravel application and you can easily get table name by using getTable() method.You will see two are lots of method available with Laravel get all columns from table.your laravel application and you can easily get all columns from table using getTable() method.

I can blow you can easily get all columns from table in laravel this example.

Example:1

Here In this exmaple laravel get all columns from table using a schema builder to a getColumnListing method

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$user= new User;

$table = $user->getTable();

$columns = \Schema::getColumnListing($table);

dd($columns);

}

Output:

array:8 [

0 => "id"

1 => "name"

2 => "email"

3 => "email_verified_at"

4 => "password"

5 => "remember_token"

6 => "created_at"

7 => "updated_at"

]

Example:2

Here In this exmaple laravel get all columns from table using a db builder to a getColumnListing method

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$user= new User;

$table = $user->getTable();

$columns = \DB::getSchemaBuilder()->getColumnListing($table);

dd($columns);

}

Output:

array:8 [

0 => "id"

1 => "name"

2 => "email"

3 => "email_verified_at"

4 => "password"

5 => "remember_token"

6 => "created_at"

7 => "updated_at"

]

I hope it can help you..


Recommended Posts