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 calledreadFromDb.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
I would select all records/documents from them into associative arrays in thereadFromDb.php
, and then includereadFromDb.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 calledreadFromDb.php
and include it on top of every function in every single controller? In that case, where should I put thisreadFromDb.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?
I am using class and call it asrepository
to handle all my request through database
First i create an fileUserRepository
<?php
namespace App\Repositories;
use App\Model\User;
class UserRepository
{
function getUsers(){
Return User::get();
}
}
Then in Controller, you just need to call therepository
in the parent ofcontruct
<?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 callLaravel Model
and do all the store, reads inside Controller and cause messy code
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 yourreadFromDb.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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.