【问题标题】:laravel query builder filters not giving resultslaravel 查询生成器过滤器不给出结果
【发布时间】:2021-04-03 15:06:37
【问题描述】:

我正在尝试输出一个视图,该视图显示求职者对特定工作的申请。到目前为止,我只能查看所有收到的应用程序的转储,但我无法实现查询以仅查看特定工作的应用程序。即我希望能够查看每个不同职位空缺的条目。谢谢

\\

    public function viewapplications(){

    $user = auth()->user()->id;
    $users = DB::table('job_applications')
    ->join('jobs', 'job_applications.job_id', '=', 'jobs.id')
    ->join('users', 'job_applications.user_id', '=', 'users.id')
    ->where('jobs.id', '=', 'job_applications.job_id')
    ->where('users.id', '=', $user)
    ->get();

    return view('jobs.viewapplications', compact('users'));
    }

\\

【问题讨论】:

  • 返回视图('jobs.viewapplications', compact('users'));
  • 您还应该在该查询中使用$id。 (建立适当的关系可能会使这更容易)
  • @SalimDjerbouh 这是一个错字,已修复但仍无法显示结果
  • @brombeer ,$id 到底是什么?
  • 说实话,我不知道,这是你的代码。你的方法有一个参数$id,那是干什么用的? ->where('jobs.id', $id) 可能代替 ->where('jobs.id', '=', 'job_applications.job_id')

标签: php mysql laravel eloquent


【解决方案1】:

当你使用 join 时,你不应该像你所做的那样做一个 where 语句。 试试这个:

$users = DB::table('job_applications')
    ->join('jobs', 'job_applications.job_id', '=', 'jobs.id')
    ->join('users', 'job_applications.user_id', '=', 'users.id')
    ->get();

此选择将为您提供所有 job_aplications,其中包含属于它们的作业和用户数据。您是什么意思“每个不同的职位空缺”?有哪些职位空缺?

【讨论】:

    【解决方案2】:

    首先,停止使用原始查询,您可以使用关系来简化此操作

    我假设您的数据库架构类似于以下内容:

    Table users:
    |---------
    | id
    | name
    |---------
    
    Table jobs:
    |---------
    | id
    | job_title
    |---------
    
    Table job_applications:
    |---------
    | id
    | job_id
    | user_id
    |---------
    

    基于关系的模型如下:

    Class Users extends Model{
        protected $table = "users";
        protected $fillable = [
            'name',
        ];
    
        public function job_applications(){
            return $this->hasMany(JobApplication::Class);
        }
    }
    
    Class Jobs extends Model{
        protected $table = "jobs";
        protected $fillable = [
            'job_title',
        ];
    
        public function job_applications(){
            return $this->hasMany(JobApplication::Class);
        }
    }
    
    Class JobApplications extends Model{
        protected $table = "job_applications";
        protected $fillable = [
            'job_id',
            'user_id',
        ];
    
        public function job(){
            return $this->belongsTo(Job::class);
        }
    
        public function user(){
            return $this->belongsTo(Users::class);
        }
    }
    

    有了这个,您现在可以轻松地在这些关系之间导航以获得结果,例如:
    我需要以下结果:-

    • 特定用户打开的所有应用程序
    • 针对特定工作打开的所有应用程序

    当我只有一个 application_id 作为这些查询的输入时

    class DoStuff extends Controller {
        //All applications opened by a specific user
        public function getUserApplicationsOpendByApplicationId($applicationId){
            $application = JobApplications::findOrFail(applicationId);
            $allApplicationsOpendByUser = $application->user()->job_applications;
    
            //return $allApplicationsOpendByUser to view
        }
    
        //All applications opened on a specific job
        public function getAllApplicationsOpendOnJobByApplicationId($applicationId){
            $application = JobApplications::findOrFail(applicationId);
            $allApplicationsOpendOnJob = $application->job()->job_applications;
    
            //return $allApplicationsOpendOnJob to view
        }
    }
    

    有关 laravel 关系的更多信息,请查看official documentation

    【讨论】:

    • 谢谢 Hefny,如果我想返回一个经过身份验证的用户的特定工作的所有应用程序??
    • @harrid easy, auth()->user()->job_applications()->where('job_id', $jobID)->get()
    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 2020-03-16
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多