6
class SampleELoq extends Model
{
    use SoftDeletes;

    public function conditionFields() {
       return $this->belongsToMany('App\EloquentModel\ConditionField');
    }
}

nameSpace is the name space of the SampleELoq

$Eloq = $nameSpace::find(1);

$table = with(new $nameSpace->conditionFields)->getTable();

print_r(Schema::getColumnListing($table));

How can i able to get the table name of the conditionFields?

goto
  • 7,908
  • 10
  • 48
  • 58
nescafe
  • 187
  • 1
  • 3
  • 14

4 Answers4

5

To get table from conditionFields you need return relation model, then you can get table by getTable method. Some like this

Model::first()->conditionFields()->getRelated()->getTable()
Captain Fail
  • 77
  • 1
  • 3
5

You don't have to retrieve a model from the database like in Catain Fail's answer: you can obtain the related table name in any situation as follows:

$relation = (new MyModel)->myRelationship(); // Returns a Relations subclass like BelongsTo or HasOne.
$relatedModel = $relation->getRelated(); // Returns a new empty Model
$tableName = $relatedModel->getTable();

Or in short:

$tableName = (new MyModel)->myRelationship()->getRelated()->getTable();
Robin De Schepper
  • 4,942
  • 4
  • 35
  • 56
  • This one worked! Related: https://stackoverflow.com/a/20812314/5113030 (*How to return database table name in Laravel*...) – Artfaith Nov 22 '22 at 13:04
1

After digging hard I found a solution. It can be achieved like this.

$tableName = (new SampleELoq)->conditionFields()->getTable();

In General

$tableName = (new MODELCLASS)->RELATIONSHIP()->getTable();
Rutvij Kothari
  • 1,253
  • 11
  • 22
  • 1
    This is incorrect and leads to the following error: `Call to undefined method Illuminate/Database/Eloquent/Relations/BelongsTo::getTable()` – Robin De Schepper May 11 '19 at 11:17
0

you have two ways :-

     dd(Model::$table);

or inside your model :-

 dd($this->table());
Ahmed farag mostafa
  • 2,802
  • 2
  • 14
  • 33