How to get table name and table column names from model in Laravel 5?

How to get table name and table column names from model in Laravel 5?

In this post, i will let you know how to get table name from model in Laravel.

According to Laravel standard structure, We do not need to tell Eloquent which table will be used for model because by convention, model is used to interact with the table and plural name of model class will be used as table name.

So sometime you may need to get table name from model into your laravel application and you can easily get table name by using getTable() method.

You will see there are lots of method available with Laravel Eloquent so using eloquent is best approach for your application.

Get Table Name From Model
  1. $product = new Product;
  2. $table = $product->getTable();
  3. print_r($table);
Get Table Column Name From Model

First i will define a method in model class by following way :

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Product extends Model
  5. {
  6. public function getTableColumns() {
  7. return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
  8. }
  9. }

Now you will get all columns of "products" table and if you need it in controller then you can get it by following way :

  1. $product=new Product;
  2. $columns=$product->getTableColumns();
  3. print_r($columns);

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort kurtköy escort kartal escort pendik escort ataşehir escort
şişli escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort