【发布时间】:2021-06-10 23:01:35
【问题描述】:
//--------------------------------------------// //下面是控制器索引功能代码 // //-------------------------------------------//
public function index($from, $to,$id)
{
$data=Attendance::where('attendance_date','>=',$from)
->where('attendance_date','<=',$to)
->with('userAttendance')//--> **I need to pass the $id to this function in the model**
->with('admin')
->get()
//------------------------------------------------// //下面是模型代码 // //------------------------------------------------//
class Attendance extends Model
{
protected $table = "attendances";
protected $fillable=[
'attendance_date',
'admin_id',
];
//---> **i need the $id passed to here**
public function userAttendance()
{
return $this->belongsToMany('App\User', 'user_attendances','attendance_id','user_id')
->withPivot(
'present_absent',
'excuse',
'attendance_key_amount',
'verified_date',
'comment',
);
}
}
【问题讨论】:
-
你想传递什么,为什么?请分享更多逻辑。
-
我在控制器中接收到来自我的路由的几个变量,其中一个名为
$id,所以我需要将这个$id传递给名为 userAttendance 的函数model 我正在使用 Attendance。我已经使用 eloquent->with(userAttendance)在控制器索引中调用了这个函数,那么如何将 控制器 中的$id变量传递给模型中的这个 函数
标签: php laravel api eloquent backend