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.

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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改