Laravel 5 Models and scoping with custom methods

143 Views Asked by At

I have a Model that I created a custom method for to retrieve data from two tables (the model and a table that is related) to avoid multiple querying. Can't say I'm an expert in Laravel, but I do ok. Here is the method: (class is Criteria)

public static function withDiagnostic()
{
    return (\DB::select(\DB::raw("SELECT productspecs.product_id AS productId, criteria.id as criteriaId,criteria.name as criteriaName,diagnostic.name as diagnosticName ".
                  "FROM criteria, diagnostic,productspecs ".
                  "WHERE productspecs.criteria_id=criteria.id ".
                  "AND productspecs.diagnostic_id=diagnostic.id ".
                  "AND criteria.diagnostic_id = diagnostic.id ".
                  "AND criteria.inactive=0 ".
                  "ORDER by diagnostic.name,criteria.name")));
}

I'd like to be able to Criteria::withDiagnostic()->where('productId',20) but I get a method where not defined.

What do I need to do (without creating a second method to do this?)

and can that custom query be re-written in Eloquent only? My Eloquent is not so good for multiple table joins and unions etc.

0

There are 0 best solutions below