0
votes

Let me show you my eloquent ORM class:

<?php

namespace App\Http\Models\Transport_Type;

use Illuminate\Database\Eloquent\Model;
use App\Http\Models\Transport_Type\TransportType;

class TransportType extends Model{

    public $timestamps = false;

    public function foo($language_id){
        $transport_types = TransportType::join('translations', 'transport_types.name', '=', 'translations.id')
                            ->join('translation_entries', 'translations.id', '=', 'translation_entries.translation_id')
                            ->where('translation_entries.language_id', '=', $language_id)
                            ->orderBy('parent_id')
                            ->get(['transport_types.id', 'transport_types.parent_id', 'translation_entries.value']);

        return $transport_types;
    }
}

As you see I have foo function which I can call from many different controllers. so here is my question: Is it a good practice? or should I move that query logic to controller?

1
I'm not sure if this is what you're looking for, but you might use a query scope instead of a regular function. These can have parameters passed as well. laravel.com/docs/master/eloquent#query-scopesparker_codes

1 Answers

1
votes

I prefer to keep my eloquent models and controllers as empty as possible, because of that I prefer to have repositories where I handle all my data access. See https://medium.com/employbl/use-the-repository-design-pattern-in-a-laravel-application-13f0b46a3dce for examples how to do this in Laravel.

Edit: I'm not sure of your approach is a bad of good practice. Just wanted to share my opinion maybe it helps :)!