0
votes

I want to get column list with Eloquent relation in laravel. When I use this comman

 $columns = Schema::getColumnListing('news');

Result is all fields of news table but I want to get relation fields for CategoryNews table.

News model:

public function NewsCategories()
   {
    return $this->belongsTo('App\CategoryNews');
   }

CategoryNews model:

public function News()
  {
    return $this->hasMany('App\News');
  }
1
Can't you just change it from 'news' to 'news_categories' or whatever your table is called? - Joel Hinz
No I want get all relationa and dynamic . this is manual - paranoid

1 Answers

1
votes

You should be able to do something like this:

$columns = Schema::getColumnListing($news->NewsCategories()->getRelated()->getTable()));

Using getRelated() method you are getting related object for relationship (in your case it's App\CategoryNews) and now using method getTable() you can get table name for this model and you can use this table name for getColumnListing() method.