0

I'm having difficulty converting this SQL query to Laravel Query Builder.I tried for hours but couldn't get my head around this.I tried online tools for converting SQL to Query builder but didn't work.

Here is my code:

SELECT 
   technologies.name_en,
   Count(cig_members.id) AS CigTotal,
   Count(CASE
           WHEN cig_members.is_ethnic = 1 THEN 1
           ELSE NULL
         END) AS CigTotalEthnic,
   Count(CASE
           WHEN cig_members.gender = 'female' THEN 1
           ELSE NULL
         END) AS CigTotalFemale,
   Count(CASE
           WHEN cig_members.gender = 'female'
                AND cig_members.is_ethnic = 1 THEN 1
           ELSE NULL
         END) AS CigTotalEthnicFemale,
   Count(farmers.id) AS NonCigTotal,
   Count(CASE
           WHEN farmers.is_ethnic = 1 THEN 1
           ELSE NULL
         END) AS NonCigtoTalEthnic,
   Count(CASE
           WHEN farmers.gender = 'female' THEN 1
           ELSE NULL
         END) AS NonCigTotalFemale,
   Count(CASE
           WHEN farmers.gender = 'female'
                AND farmers.is_ethnic = 1 THEN 1
           ELSE NULL
         END) AS NonCigtTotalEthnicFemale
   FROM   adopting_technologies
   JOIN adopting_farmers
          ON adopting_farmers.id = adopting_technologies.adopting_farmer_id
   LEFT JOIN cig_members
          ON cig_members.id = adopting_farmers.cig_member_id
   LEFT JOIN farmers
          ON farmers.id = adopting_farmers.farmer_id
   LEFT JOIN financial_years
          ON financial_years.id = adopting_farmers.financial_year_id
   LEFT JOIN technologies
          ON technologies.id = adopting_technologies.technology_id
   GROUP  BY adopting_technologies.technology_id

How can I convert this?

2
  • Do you already have a model for adopting_technologies ? Or wanna use generic ORM? Commented Jun 1, 2021 at 9:59
  • @RobBiermann I've model for "adopting_technologies" Commented Jun 1, 2021 at 10:02

3 Answers 3

2

It should look roughly like this:

$result = AdoptingTechnology::selectRaw("
        technologies.name_en,
   Count(cig_members.id) AS CigTotal,
   Count(CASE
           WHEN cig_members.is_ethnic = 1 THEN 1
           ELSE NULL
         END) AS CigTotalEthnic,
   Count(CASE
           WHEN cig_members.gender = 'female' THEN 1
           ELSE NULL
         END) AS CigTotalFemale,
   Count(CASE
           WHEN cig_members.gender = 'female'
                AND cig_members.is_ethnic = 1 THEN 1
           ELSE NULL
         END) AS CigTotalEthnicFemale,
   Count(farmers.id) AS NonCigTotal,
   Count(CASE
           WHEN farmers.is_ethnic = 1 THEN 1
           ELSE NULL
         END) AS NonCigtoTalEthnic,
   Count(CASE
           WHEN farmers.gender = 'female' THEN 1
           ELSE NULL
         END) AS NonCigTotalFemale,
   Count(CASE
           WHEN farmers.gender = 'female'
                AND farmers.is_ethnic = 1 THEN 1
           ELSE NULL
         END) AS NonCigtTotalEthnicFemale
         ")
            ->join('adopting_farmers', 'adopting_farmers.id','adopting_technologies.adopting_farmer_id')
            ->leftJoin('cig_members','cig_members.id','adopting_farmers.cig_member_id')
            ->leftJoin('farmers','farmers.id','adopting_farmers.farmer_id')
            ->leftJoin('financial_years','financial_years.id','adopting_farmers.financial_year_id')
            ->leftJoin('technologies','technologies.id','adopting_technologies.technology_id')
            ->groupBy('adopting_technologies.technology_id')
            ->get();

If you have any issues let me know:) ofc cannot test it locally

Sign up to request clarification or add additional context in comments.

Comments

1
DB::table('adopting_technologies')
            ->select('adopting_technologies.technology_id', 'technologies.name_en')
            ->selectRaw("
                COUNT(cig_members.id) AS CigTotal,
                COUNT(CASE WHEN cig_members.is_ethnic = 1 THEN 1 ELSE NULL END) AS CigTotalEthnic,
                COUNT(CASE WHEN cig_members.gender = 'female' THEN 1 ELSE NULL END) AS CigTotalFemale,
                COUNT(CASE WHEN cig_members.gender = 'female' AND cig_members.is_ethnic = 1 THEN 1 ELSE NULL END) AS CigTotalEthnicFemale,
                COUNT(farmers.id) AS NonCigTotal,
                COUNT(CASE WHEN farmers.is_ethnic = 1 THEN 1 ELSE NULL END) AS NonCigtoTalEthnic,
                COUNT(CASE WHEN farmers.gender = 'female' THEN 1 ELSE NULL END) AS NonCigTotalFemale,
                COUNT(CASE WHEN farmers.gender = 'female' AND farmers.is_ethnic = 1 THEN 1 ELSE NULL END) AS NonCigtTotalEthnicFemale
            ")
            ->join('adopting_farmers', 'adopting_farmers.id', '=', 'adopting_technologies.adopting_farmer_id')
            ->leftJoin('cig_members', 'cig_members.id', '=', 'adopting_farmers.cig_member_id')
            ->leftJoin('farmers', 'farmers.id', '=', 'adopting_farmers.farmer_id')
            ->leftJoin('financial_years', 'financial_years.id', '=', 'adopting_farmers.financial_year_id')
            ->leftJoin('technologies', 'technologies.id', '=', 'adopting_technologies.technology_id')
            ->groupBy('adopting_technologies.technology_id')
            ->get();

4 Comments

You are using the wrong joins so this will corrupt the result
@RobBiermann sorry, overlooked that. Upated answer.
And the user said he has a model, so need to use ORM :)
@RobBiermann See my answer below for an Eloquent approach.
0
class adopting_technologies extends Model
{
    public function newCollection(array $models = [])
    {
        return new FarmerCollection($models);
    }
    public function adopting_farmers()
    {
        return $this->belongsTo(adopting_farmers::class);
    }
    // other relations for 'cig_members', 'farmers', 'financial_years' and 'technologies'
}
namespace App\Collections;

use Illuminate\Database\Eloquent\Collection;
 
class FarmerCollection extends Collection
{

    public function cigTotal()
    {
        return $this->pluck('cig_members')->map(function($items) { return $items->count(); })->sum();
    }
    // other totals for cigTotalEthnic, CigTotalFemale, CigTotalEthnicFemale, NonCigTotal, NonCigtoTalEthnic, NonCigTotalFemale, NonCigtTotalEthnicFemale
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.