我们得到一种关系。现在我们在关系上设置条件以获取状态并针对每个案例一一检查。获取状态类型后,状态类型的值将传递给另一个关系。我们应该怎么做?下面是一个例子。
/////// Model function use for relationship//////
public function Name($statusType){
$value='';
switch ($statusType){
case 0:
$value= $this->belongsTo(/////code//////);
break;
case 1:
$value= $this->belongsTo(/////code//////);
break;
case 6:
$value= $this->belongsTo(/////code//////);
break;
case 2:
$value= $this->belongsTo(/////code//////);
break;
case 3:
$value= $this->belongsTo(/////code//////);
break;
case 4:
$value= $this->belongsTo(/////code//////);
break;
}
return $value;
}
这是我的控制器功能
$Variable = ModelName::where('id', $id])->with(['RelationShipName' => function ($query) use ($startDate) {
$query->whereDate('date', '>=', $startDate)->orderBy('date', 'DESC');
},'RelationShipName.anotherRelationShipName'])->get();
例子
$Variable = ModelName::where('id', $id])->with(['RelationShipName' => function ($query) use ($startDate) {
////how to get statusType and pass argument///
$query->whereDate('date', '>=', $startDate)->orderBy('date', 'DESC');
},'RelationShipName.anotherRelationShipName(argument)'])->get();