lundi 27 février 2017

Laravel: how I can write my onw function with own query on my model?

Example: I wanna create my own function for laravel model like:

(original query)

SELECT
   AVG(persons_positions.points)
FROM
    positions
INNER JOIN persons_positions ON (
    positions.id = persons_positions.position_id
)
WHERE positions.country = 1
AND persons_positions.person_id = 2

(model class)

class Menedzher extends Model {
    function oh ($x) {
        return DB::table('positions')
            ->join('persons_positions', 'positions.id', '=', 'persons_positions.position_id')
            ->where('positions.country', $x) // see here
            ->where('persons_positions.person_id', '=', $this->id ) // see it!!
            ->select(DB::raw('AVG(persons_positions.hits)'));

    }
}

and to use it:

Menedzher::get(1)->oh(3)

Thanks!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mxs8Lu
via IFTTT

Aucun commentaire:

Enregistrer un commentaire