在作业模型中:
公共函数jobApplications((
{
返回$this->hasMany(JobApplication::class,'job_id'(;
}
在JobApplication模型中
公共函数jobs((
{
返回$this->belongsTo(Job::class,'Job_id'(;
}
在job_applications迁移中
$table->id((
$table->foreignId("job_id"(->受约束的("作业"(
$table->foreignId("user_id"(->受约束的("用户"(
$table->文本("标记"(->nullable((
$table->unsignedInteger("状态"(->违约(1(
我需要获取所有作业及其作业应用程序,其中job_applications.status=(用户输入状态(和job_application.user_id=已验证的用户id。我该如何获取
下面是我尝试的语法,它返回了未定义的变量状态
$jobs=作业::其中("状态",1(
$status=$request->地位
if($status({
$jobs=$jobs->whereHas('jobApplications',function($q({
$q->where('status',$status(;
$q-<where('user_id',Auth((->user((->id(;
}(
返回$jobs->get((
有人能提出解决方案吗?
如果使用closer,则无法访问closer外部定义的变量。所以你应该用它。像这样。
if($status){
$jobs = $jobs->whereHas('jobApplications', function($q) use($status){
$q->where('status',$status);
$q->where('user_id',Auth()->user()->id);
});
这将删除未定义的veriable$状态错误。
要在闭包方法中使用外部变量,您需要使用"使用";关键词类
if($status){
$jobs = $jobs->whereHas('jobApplications', function($q) use ($status) {
$q->where('status',$status);
$q->where('user_id',Auth()->user()->id);
});}
return $jobs->get();