samedi 22 juillet 2017

Laravel 5.4 : Writing SQL query in Model

I am working on a small laravel project where I use to write all my sql queries in the controller and then sent the data to the view.

This is a function from my controller:

 public function view_all_applicants($job_id,$job_title)
    {

        $sql = DB::select("SELECT jm.`jobseeker_id`,jm.`jobseeker_name`,jm.`jobseeker_phone`,jm.`login_email`,jm.`jobseeker_coverletter`,jm.`jobseeker_cv`,
                  jm.`jobseeker_education`,jm.`jobseeker_experience`,jm.`jobseeker_linkedin`,aj.`cover_letter`,aj.`date_applied`
                  FROM `jobseeker_main` AS jm
                  JOIN `applicants_for_job` AS aj ON jm.`jobseeker_id`=aj.`jobseeker_id`
                  JOIN `job_details` AS jd ON jm.`jobseeker_id`=jd.`job_id`
                  WHERE aj.`job_id`=$job_id");

        $count = DB::select("SELECT COUNT(DISTINCT(aj.`afj_id`)) AS total FROM `jobseeker_main` AS jm
                             JOIN `applicants_for_job` AS aj ON jm.`jobseeker_id`=aj.`jobseeker_id`
                             JOIN `job_details` AS jd ON jm.`jobseeker_id`=jd.`job_id` WHERE aj.`job_id`=$job_id");

        foreach($count as $ss)
        {
            $data['total'] = $ss->total;
        }

        return view('company.applicants_for_job',$data,compact('sql'));     

    }

As you can see I am running two sql queries here and then I sent the data to the views. My question is, is there anyway where I can write my queries in Model (instead of controller) and then sent the result to the views? I am new to Laravel. Please Help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire