Laravel Get All Columns From Table Example - ItWebtuts

getTable() method

Hi dev,

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

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..

No comments:

Post a Comment