php - In Laravel project, where do I put code which reads data from database tables, which would be common to multiple controllers?

Background:

I am having my first encounter with any MVC framework, as I am developing a little application with PHP web framework Laravel v6 and MongoDB (with jenssegers moloquent) as database engine. I am following this tutorial series to learn Laravel 6.

Normally when I used to develop such applications with simple php, I would create one file called readFromDb.php and in it I would find/read/select data from all DB tables (collections in Mongodb). Then I would include in on top of every PHP file in which I would need to do some processing on any data from DB.

For example, if I have the following collections

  1. allPaintingsCollection
  2. paintingHistoriesCollection
  3. paintingCategoriesCollection
  4. artGalleriesCollection
  5. paintingArtistsCollection
  6. supervisorArtistsCollection
  7. smPlatformsCollection
  8. nonSmPlatformsCollection
  9. targetSchoolsCollection

I would select all records/documents from them into associative arrays in the readFromDb.php, and then include readFromDb.php on top of every page where I would need to display or do processing on data from DB.

Question:

Now, in Laravel, should I create such a script called readFromDb.php and include it on top of every function in every single controller? In that case, where should I put this readFromDb.php file, and how do I include it in controllers?

Or should I write code to read from relevant DB collection/table in each function in every controller, before using that data from DB?

Answer

Solution:

I am using class and call it as repository to handle all my request through database

First i create an file UserRepository

<?php

namespace App\Repositories;

use App\Model\User;

class UserRepository
{
    function getUsers(){
       Return User::get();    
    }
}

Then in Controller, you just need to call the repository in the parent of contruct

<?php

namespace App\Http\Controllers;
use App\Repositories\UserRepository;

class UserController extends Controller
{
    protected $user;
    function __construct(){
        $this->user = new UserRepository();
    }

    function index(){
        $users = $this->user->getUser(); // getUser() is the function inside the repository class
        return view('user',compact('users'));
    }

}

That's how I do code in laravel, because i dont want to call Laravel Model and do all the store, reads inside Controller and cause messy code

Answer

Solution:

Laravel has Eloquent ORM what presents Object Relation Mapping interface.

In Introduction:

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Basically, same as your readFromDb.php file but it handles with OOP so codebase is very understandable and expandable and strictly related with your code.

When querying with Eloquent you use Models. If your query result has single row you get single Model, if your result has multiple rows you get Eloquent\Collection result set contain your models.

Finally, you mustn't write ORM in Laravel because you already have.

If you think high level abstract accessors; you should explore design patterns. After that you can think to Repository Pattern, Decorator Pattern etc.

Source