Link to home
Start Free TrialLog in
Avatar of Matthew B
Matthew BFlag for Canada

asked on

Where to store my raw queries for Laravel Project?

I have some complex queries with many joins i am using in a laravel project.
Currently all queries are in my controller which i want to refactor into a App\Queries namespace.

I am stuck on the app flow, would i call these queries from my model, or my controller directly?

would this be a correct structure?

<?php 
namespace App\Queries;
//App\Queries\GetClient;

class Client {

public function get($cID){

$client = DB::select( DB::raw("

select
client_id,
client_name,
client_phone,
client_address
from clients
where client_id = :cID"
), array("cID" => $cID));

return $client;

}

}

Open in new window


then how would i call this query from my controller/model?

<?PHP

//call from model ??
//call directly from controller??

public function show(Request $cID){

$client = new App\Queries\GetClient->get();

//return to view or what ever

}

Open in new window


does this make sense ? am i way off?
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

That looks fine, have you tried it?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.