【发布时间】:2022-07-22 16:11:56
【问题描述】:
在工作模型中:
公共函数 jobApplications()
{
return $this->hasMany(JobApplication::class, 'job_id');
}
在 JobApplication 模型中
公共函数jobs()
{
return $this->belongsTo(Job::class, 'job_id');
}
在 job_applications 迁移中
$table->id();
$table->foreignId("job_id")->constrained("jobs");
$table->foreignId("user_id")->constrained("users");
$table->text('remarks')->nullable();
$table->unsignedInteger('status')->default(1);
我需要获取所有工作及其工作申请,其中 job_applications.status =(用户输入状态)和 job_applications.user_id =authenticated users id。我怎样才能得到它?
下面是我尝试过的语法,它返回未定义的变量状态
$jobs = Job::where('status',1);
$status =$request->状态;
如果($状态){
$jobs = $jobs->whereHas('jobApplications', function($q){
$q->where('status',$status);
$q->where('user_id',Auth()->user()->id);
});
返回 $jobs->get();
任何人都可以提出解决方案吗?
【问题讨论】:
-
不相关,但如果 JobApplication 有
public function job()方法,因为它是一个 belongsTo,这不是更有意义。
标签: laravel eloquent relationship where-clause has-many