Multiple Models From Single Controller

- 1 answer

I'm new to laravel and a bit rusty in general so bear with me. I'm wondering what the best way to do this is.

I have several tables and several mysql views. Each table or view has its own Model (is that right?).

Tables: User, Department, Site, UserTime

Views: FullUser (joining User, Department and Site), UserTime (joining User, UserTime), UserDateOnly(joining User, UserTime and grouped on date/user)

For each supervisor (which is flagged in User) I need to list all of that supervisors employees (linked on User) and for each employee list all of their times grouped by date. So basically the result wil be a list of employees with a list of dates for each and each date will have a list of several times.

Firstly, should I add this method to the User controller or create a seperate controller for this as it will be querying several models?

Secondly, my plan for the process is:

  1. Get list of all relevant employees for the supervisor from FullUser.
  2. Loop through results and query the UserDateOnly view for each to get all dates for that user.
  3. Loop through results and query the UserTime view for each date to get all times for that date, for that user.
  4. Add the results to a collection in this layout to pass to the view:





Does this seem sensible and should I be doing all this in one control method. I cant figure another way to do it without doing queries from the View.




Controller are just files to have a better clean code, but they're not really assigned to a Model, you can have UserController but throw whatever you want in there and get whatever model you want from there:

class UserController {

   public function getPhotos(Request $request, Photo $photo){
       return $photo;


You're question is more about database queries, which can be done through models (Eloquent) for example if you want to get the User of type supervisors, assuming you have that as a column in your table

User::where('is_supervisor', 1)->get() // Get all;
User::where('is_supervisor', 1)->first() // get the first result row in db

Now if you want to make it more complex and want to go into Eloquent Relations, then that's a whole new topic you need to read about.

For example you can assign departments for users and vice-versa like so

Have the migrations ready (database tables)

For the users migration file

Here you need to add a column called department_id, if the user belongs to a single department.

    // appending to users migrations 

Then in Users.php model you need to add this

public function department(){
   return $this->belongsTo('App\Department'); // or $this->belongsTo(Deparment::class); if you want to put use `App\Deparments` in the head of your php model file

Then you can get the department object from the user object like so, in controller if you want:

$User = User::first()->department;

This code above will try to select the deparment row from the department table in your database based on the value in the selected user's department_id, so if you have a user of id 1 and department_id of 2, and you tell Laravel to get the user object of id of 1, then access the ->department relation, it will fetch that based on the id specified, and for that it will return the deparment object of id of 2, doing so you can then access the data of that department object, like you can do : User::first()->department->title that will get you the title column from departments.

It's a long topic of lots of possibility, i would suggest you read Laravel Relations for that.

Your best friend here is and will always be the dd helper, whenever you doubt the output of your code just dd() it like so, if you want to see what this User::first()->department have you can dd(User::first()->department); and see the data when in doubt.