当前位置:网站首页>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 .
边栏推荐
- Defi "where does the money come from"? A problem that most people don't understand
- 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
- Jetpack Compose 实现完美屏幕适配
- Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集
- 【刷题篇】供暖器
- 想请教一下,我在佛山,到哪里开户比较好?手机开户是安全么?
- Race of golang
- [Title brushing] heater
- Exlipse operates on multiple rows at the same time. For example, input the same text in multiple lines and columns at the same time
猜你喜欢

编程实战赛来啦!B站周边、高级会员等好礼送你啦!

Shell programming overview

Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage

Intelligent operation and maintenance: visual management system based on BIM Technology

The first three passes of sqli Labs

What is erdma as illustrated by Coptic cartoon?
![[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?

Waving flags and shouting for basic language
![[kali] Kali system, software update (with image source)](/img/ac/43a3f81d50ab6866271b500b142252.png)
[kali] Kali system, software update (with image source)

Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance
随机推荐
"As a service", the inevitable choice of enterprise digital transformation
Rpm2rpm packaging steps
Comprehensively analyze the basic features and summary of free and paid SSH tools
数据恢复软件EasyRecovery15下载
【系统分析师之路】第五章 复盘软件工程(敏捷开发)
Step by step | help you easily submit Google play data security form
Geoffreyhinton: my 50 years of in-depth study and Research on mental skills
Implementation of forwarding server using IO multiplexing
重磅:国产IDE发布,由阿里研发,完全开源!
【刷题篇】爱吃香蕉的珂珂
数字化转型道阻且长,如何迈好关键的第一步
ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
LeetCode_ Stack_ Medium_ 227. basic calculator II (without brackets)
In the digital age, XDR (extended detection and response) has unlimited possibilities
Golang template (text/template)
Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
@ResponseBody的作用
Mysql database foundation: stored procedures and functions
How to execute a query SQL
Crypto questions