doubu8643 2016-04-03 08:24 采纳率: 100%
浏览 46
已采纳

在laravel中获取具有Eloquent关系的列列表

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条回答 默认 最新

  • dongyuan7110 2016-04-03 10:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?