given some code:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class Reception extends Model
{
use HasFactory;
protected $table = 'receptions';
/**
* get all with repetitive info
*/
public function getAllWithRepetitiveInfo() : Collection
{
return DB::table('receptions')->leftJoin('receptions_repetitives', 'receptions_repetitives.reception_id', '=', 'receptions.id')->get();
}
}
this getAllWithRepetitiveInfo would be used somewhere in my controller, but… how? Seeing the documentations: https://laravel.com/docs/10.x/eloquent all I see were static calls. I dont want that…
here https://github.com/alexeymezenin/laravel-best-practices I saw something similar:
public function index()
{
return view(‘index’, [‘clients’ => $this->client->getWithNewOrders()]);
}
but of course following this logic: $this->reception won’t work (property doesn’t exist) and I dont know how it would even be injected.
So all in all, I want to avoid Reception::getAllWithRepetitiveInfo(), I want to do $this->reception->getAllWithRepetitiveInfo()
>Solution :
You inject your model as dependency (in constructor or method or other) then you can call it like that you want:
class SomeController extends Controller {
public function __construct(private Reception $reception) {}
public function index() {
dd($this->reception->getAllWithRepetitiveInfo());
}
}