Laravel 的 ORM 缓存包
LaraCache是一个基于 ORM 的 Laravel 包,用于基于模型查询创建、更新和管理缓存项。使用此包,您可以缓存在整个应用程序中大量使用的查询。
use Mostafaznv\LaraCache\Traits\LaraCache; class Article extends Model { use LaraCache; public static function cacheEntities(): array { return [ CacheEntity::make('list.forever') ->cache(function() { return Article::query()->latest()->get(); }), CacheEntity::make('latest') ->validForRestOfDay() ->cache(function() { return Article::query()->latest()->first(); }) ]; } }
使用该…
18 个 Laravel 8 数据库查询优化建议
Laravel 8更改密码功能实现
In this tutorial, we will discuss Change Password Functionality using Laravel 8. Change passwords mostly use carries a lot of sensitive and important data, so keeping work data safe is a major priority. Laravel Authentication default provided Change password functionality.…
原laravel 如何覆盖composer的 vendor类文件?
作为 Laravel 开发人员,你可能遇到过这样的情况:你不喜欢 vendor 文件夹某个包中的某些代码,并且想要更改它。你可以直接在该文件中更改它。但问题是当你点击composer update命令升级包时很容易覆盖已修改的vendor类文件。
那么有什么解决办法呢?
感谢Composer,它具有覆盖任何包/类的功能。Composer 使用PSR-4来加载类。因此,在 composer.json 中,你可以提及从哪些文件或文件夹加载类。同样你也可以排除加载它。
操作步骤:
windows用户
排除文件
请参考下面的示例以了解我如何从包tymon/jwt-auth 中排除 1个文件
"exclude-from-classmap": ["vendor\\tymon\\jwt-auth\\src\\Middleware\\BaseMiddleware.php"],
在上面的例子中可以看出我已经排除了BaseMiddleware.php
文件。你必须将此行放在composer.json的autoload段中。这里要注意的一件事是,我在路径中有双反斜杠,那是因为在 Windows 机器上,并且因为它是 JSON 文件,所以我必须在那里转义 (\) ,即写上双反斜杠。
包含文件以覆盖vendor
现在我们已经排除了我们想要覆盖的文件,现在需要包含有更改的新文件,以便 composer 知道要包含哪些文件。
为此,向 composer.json 中的 'psr-4' …
How Livewire works (a deep dive)
The experience of using Livewire seems magical. It’s as if your front-end HTML can call your PHP code and everything just works.
A lot goes into making this magic happen. Let me show you what’s going on:
Our Example
For …
Laravel 前端工作流程
Let's review a general workflow that you might adopt for your own projects.
Step 1: Install Laravel
laravel new my-app
… Step 2: Install Node Dependencies
Laravel框架中使用 Repository 模式
若将数据库逻辑都写在model,会造成model的肥大而难以维护,基于SOLID原则,我们应该使用Repository模式辅助model,将相关的数据库逻辑封装在不同的repository,方便中大型项目的维护。
Version:Laravel 5.1.22
数据库逻辑
在CRUD中,CUD比较稳定,但R的部分则千变万化,大部分的数据库逻辑都在描述R的部分,若将数据库逻辑写在controller或model都不适当,会造成controller与model肥大,造成日后难以维护。
Model
使用repository之后,model仅当成Eloquent class即可,不要包含数据库逻辑,仅保留以下部分:
- Property:如$table,$fillable…等。
- Mutator:包括mutator与accessor。
- Method:relation类的method,如使用hasMany()与belongsTo()。
- 注释:因为Eloquent会根据数据库字段动态产生property与method,等。若使用Laravel IDE Helper,会直接在model加上@property与@method描述model的动态property与method。
User.php
-
app/User.php
-
namespace MyBlog;
-
use Illuminate\Auth\Authenticatable;
-
use Illuminate\Database\
Laravel核心解读–服务提供器(ServiceProvider)
服务提供器是所有 Laravel 应用程序引导中心。你的应用程序自定义的服务、第三方资源包提供的服务以及 Laravel 的所有核心服务都是通过服务提供器进行注册(register)和引导(boot)的。
拿一个Laravel框架自带的服务提供器来举例子…
优化laravel数据库查询的 18 个提示
If your application is running slow or making a lot of database queries, follow the below performance optimization tips to improve your application loading time.
1. Retrieving Large Datasets
This tip mainly focuses on improving the memory usage of your
近期评论