How to get all columns from table in Laravel?

Share Me
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares
How to get all columns from table in Laravel

Hi Dev’s, Today now in this article, i will give a example of how get all columns of a table in laravel 8 application. Now here we are pick up all columns of model in laravel 8 application. So we can get all column names of a table in php laravel 8. Here we know How to get all columns from table in Laravel

Now so sometime we may need to get table name from controller into our laravel application and  that’s time we can do this very easily get table name by using getTable() method in our application. Here we will see two methods from a lots of method available with Laravel get all columns from table. Now our laravel application and we can easily get all columns from a table by using getTable() method.

Now here I will give you a blow example that’s can easily get all columns from table in laravel.

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

Now here In this example in laravel can get all columns from table by 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"
]

Read Also : How to remove element from an Array in JavaScript?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →