当前位置:网站首页>Use of laravel repository mode
Use of laravel repository mode
2022-06-30 14:11:00 【Ebony feed feed Wayne yetun wyuwenmu Bian OSAS】
During coding Decoupling has always been a hot topic . Use MVC When developing design patterns , If you need to query the database / When operating the database, you have to directly reference the model , Call model . Follow the normal call method directly as shown below , Don't use Eloquent ORM You can't operate the database , So that is ORM It has a very large coupling with this controller .
$position = Position::createPosition($params);
$position->users()->attach($userParams);
$position->permissions()->attach($permissionParams);The controller aspect should be only return related Code that does not contain any logic , So in order to decouple, we should reference repository Design patterns .
repository Required development level
First we need to define an interface
<?php
namespace App\Http\Repositories\Interfaces;
use App\Http\Repositories\Interfaces\BaseRepositoryInterface;
interface UserRepositoryInterface extends BaseRepositoryInterface
{
}
You can build a basic level by yourself BaseInterface To encapsulate common and basic operation models , After creating the interface, start binding repository To implement the interface
<?php
namespace App\Http\Permission\Repositories\Eloquent;
use App\Http\Repositories\Eloquent\EloquentBaseRepository;
use App\Http\Permission\Repositories\Interfaces\UserRepositoryInterface;
class UserRepository extends EloquentBaseRepository implements UserRepositoryInterface
{
}
After you create it, you need to create it in ServiceProvider Register and bind the interface , Ensure that it is associated with the model layer .
$this->app->bind(UserRepositoryInterface::class,function (){
return new UserRepository(new User);
});After binding, you can create service Then use the constructor to convert the interface Into it You can write logic and related codes .
边栏推荐
- Lucky hash quiz system development (source code deployment) fun investment hash game play development (case requirements)
- @ResponseBody的作用
- ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
- SQL programming problem, test case failed
- Flat shading with unity
- Race of golang
- Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
- Problems in QT creator (additional unknown and error lines are listed in the debug output window)
- Zend studio how to import an existing project
- Knowledge dissemination cannot replace professional learning!
猜你喜欢

Optimization of unit test efficiency: why test programs? What are the benefits of testing?

Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone

Getting started with shell Basics

Waving flags and shouting for basic language

How to execute a query SQL

Shell programming overview
![[Title brushing] heater](/img/ee/70e122b1b1a406624aa7c6442fcdc1.png)
[Title brushing] heater
![[observation] as the intelligent industry accelerates, why should AI computing power take the lead?](/img/61/b446a616e86247507c27390505dc6b.jpg)
[observation] as the intelligent industry accelerates, why should AI computing power take the lead?

Crypto questions

go channel && select
随机推荐
What is erdma as illustrated by Coptic cartoon?
Jetpack compose for perfect screen fit
Go sync. WaitGroup
Pytorch查看模型参数量和计算量
When SQL queries are performed in table storage, an error is reported when the primary key is added to the query result, and the query result exceeds 10W rows. Do you want to add multiple indexes to t
Pytoch viewing model parameter quantity and calculation quantity
I'd like to ask you, where can I open an account in Foshan? Is it safe to open a mobile account?
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
(8) JMeter component detailed once only controller
Geoffreyhinton: my 50 years of in-depth study and Research on mental skills
Initial attack and defense world Misc
Talk about Vue's two terminal diff algorithm, analysis of the rendering principle of the mobile terminal, and whether the database primary key must be self incremented? What scenarios do not suggest s
@ResponseBody的作用
深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
Crypto questions
org.json.JSONObject对象转json,json新增元素,根据json的key获取值。以及list对象格式字符串转jsonArray
numpy 创建空数组 data = np.empty(shape=[1, 64,64,3])
Go language mutex lock
This article explains the concepts of typed array, arraybuffer, typedarray, DataView, etc
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video