laravel - 在视图 laravel 中传递具有关系的数据
问题描述
如果我有这个模型关系
索赔:此表有很多(退款)
退款:带有各种要求的数据。此表 belongsTo(Claims) AND belongsToMany(Services)
服务:带有服务列表。此表属于ToMany(退款)
Refunds-Services:退款和服务的桥接表
索赔模型
class Claims extends Model
{
public $primaryKey = 'id';
protected $fillable = [
'dossier',
'date_cla',
];
public function refunds()
{
return $this->hasMany(Refunds::class);
}
}
退款模式
class Refunds extends Model
{
public $primaryKey = 'id';
protected $fillable = [
'date_ref',
'status_ref',
];
public function services()
{
return $this->belongsToMany(Services::class)->withPivot(['services_id','services_amount','services_status']);
}
public function claims()
{
return $this->belongsTo(Claims::class,'claims_id');
}
}
服务模式
class Services extends Model
{
public $primaryKey = 'id';
protected $fillable = [
'code',
'name',
];
public function refunds()
{
return $this->belongsToMany(Refunds::class);
}
}
在我的控制器中这个方法
if ($request->ajax()) {
$dossier = request('dossier');
$claim = Claims::with('refunds')
->where('dossier', '=', $dossier)->first();
$
$view = view('pages.modify', compact('claim'));
$sections = $view->renderSections()['table'];
return $sections;
}
如何在 Controller 中插入 Refunds 和 Service 之间的关系以使用枢轴(service_amount 在 Refunds-Services 表中)
<li>- {{ $item->pivot->services_amount }}</li>
因为现在在视图中看不到这种关系 Thx
解决方案
您可以使用嵌套急切加载
$claim = Claims::with('refunds.services')
->where('dossier', '=', $dossier)->first();
根据文档:
要急切加载嵌套关系,您可以使用“点”语法。例如,让我们在一个 Eloquent 语句中急切地加载本书的所有作者和作者的所有个人联系人:
$books = App\Book::with('author.contacts')->get();
推荐阅读
- typescript - 在 Typescript 中使用泛型强制执行相同的参数列表
- javascript - 如何更改制表器中特定列的列标题背景?(解决了)
- sql - 从雪花外部阶段选择行时如何使用正则表达式?
- java - AWS Kinesis 增强型扇出 Java 示例
- mongodb - mongodb高资源使用和配置提示
- json - aws cli 查询以查找最后拍摄的快照、拍摄日期、使用的标签(如果有)、实例的名称标签和实例 ID
- html - 粘贴后如何检查输入值的长度?
- php - 两个 laravel 应用程序干扰 xampp
- python - Django:无法验证 MultipleChoiceField
- python - 本地 postgresql 环境不与 sqlalchemy 连接