[英]Get column list with Eloquent relation in laravel
I want to get column list with Eloquent relation in laravel. 我想在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. 结果是新闻表的所有字段,但我想获取CategoryNews表的关系字段。
News model: 新闻模型:
public function NewsCategories()
{
return $this->belongsTo('App\CategoryNews');
}
CategoryNews model: 类别新闻模型:
public function News()
{
return $this->hasMany('App\News');
}
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. 使用
getRelated()
方法,您可以获取关系的相关对象(在本例中为App\\CategoryNews
),现在使用方法getTable()
,可以获取此模型的表名,并且可以将该表名用于getColumnListing()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.