当前位置: 首页 > news >正文

Laravel5.7的一些用法


1、事件需要运行
php artisan queue:work

2、数据库对象关联
1对1 hasOne
1对多 hasMany
1依赖多  belongsTo
多依赖多 belongsToMany 


3、
关联查询
with

关联统计
withCount

统计时指定字段名。

如:
withCount(['cardHolderOrders as order_count']);


4、
// 一次查询,判断昵称、手机号、会员ID 是否重复
if (!empty($id)) {
    $checkName = AdminModel::query()->where('id', '<>', $id);
} else {
    $checkName = AdminModel::query();
}

$checkName->where(function ($query) use ($nickname, $mobile, $memberId) {
    $query->when($memberId , function ($q) use($memberId) {
        $q->orWhere('member_id', $memberId);
    })->when($nickname , function ($q) use($nickname) {
        $q->orWhere('nick_name', $nickname);
    })->when($mobile , function ($q) use($mobile) {
        $q->orWhere('mobile', $mobile);
    });
});

查出结果后,还需要再依次判断,是哪一个字段重复了。
例如:

if($checkName->first()){
    $user = $checkName->first();

    // 判断昵称
    if($user['nick_name'] == $nickname) {
        throw new ApiException("昵称已存在");
    }

    // 判断手机号
    else if($user['mobile'] == $mobile) {
        throw new ApiException("手机号已存在");
    }

    // 判断成员关系
    elseif(!empty($memberId) && $user['member_id'] == $memberId) {
        throw new ApiException("已绑定其他用户");
    }
}


5、
怎样无侵入的限制用户针对某个数据表的访问权限。

#新建全局作用域,放到namespace对应文件夹里。

<?php
namespace App\Scopes;

/**
 * 用于 无侵入式过滤 权限。
*/

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class DynamicShopFilterScope implements Scope
{
    protected $searchColumn;
    protected $ids;

    public function __construct($searchColumn, $ids)
    {
        $this->searchColumn = $searchColumn;
        $this->ids = $ids;
    }

    public function apply(Builder $builder, Model $model)
    {
        if (!empty($this->ids)) {
            $builder->whereIn($this->searchColumn, $this->ids);
        }
    }
}

#新建中间件,放到namespace对应文件夹里。

<?php
namespace App\Http\Middleware;

/**
 * 用于控制哪些功能,需要 后台用户的权限。
 */

use App\Scopes\DynamicShopFilterScope;
use Closure;

class ApplyShopFilter
{
    public function handle($request, Closure $next)
    {
        $ids = request()->input('tokenInfo')['ids'];

        // 添加用于过滤的全局作用域
        // 某列表
        YourModel::addGlobalScope(
            new DynamicShopFilterScope('id', $ids)
        );

        // 某列表
        YouTwoModel::addGlobalScope(
            new DynamicShopFilterScope('shop_id', $ids)
        );
        
        return $next($request);
    }
}

在需要限制的功能的路由中,加上中间件。
这样,对应功能中,对应Model的所有操作就会加上限制了。


 

相关文章:

  • DuckDB:现代数据分析的“SQLite“内核革命
  • 人类社会的第四阶段
  • web字符转义
  • 特伦斯智慧钢琴:开启智能钢琴新体验
  • 国产免费工作流引擎star 5.9k,Warm-Flow版本升级1.7.0(新增大量好用功能)
  • FreeMarker语法深度解析与Node.js集成实践指南
  • 使用Nestjs, Bun 和 NCC 打造高效的 Node.js 应用构建流程
  • 高校毕业论文管理系统小程序实现
  • Spring MVC 多个拦截器的执行顺序
  • AI测试工具Testim——告别自动化测试维护难题
  • 开源|上海AILab:自动驾驶仿真平台LimSim Series,兼容端到端/知识驱动/模块化技术路线
  • git每次push都要输入用户名和密码很繁琐,只在第一次输入之后都不需要的解决方法
  • 企业架构之旅(1):TOGAF 基础入门
  • AI如何重塑DDoS防护行业?六大变革与未来展望
  • 【计算机网络分类全解析】从局域网到广域网的工程实践
  • 生成式人工智能认证(GAI认证)要学哪些知识?
  • DigitalOcean推出Valkey托管缓存服务
  • Pikachu靶场-PHP反序列化漏洞
  • 缓存并发更新的挑战
  • FastAPI中使用Pydantic进行数据验证的示例与关键技术点分析
  • 女乘客遭顺风车甩客、深夜丢高速服务区,滴滴霸道回应:赔五百元
  • 洛阳原副市长收礼品消费卡,河南通报6起违反八项规定典型问题
  • 持续更新丨伊朗官员:港口爆炸已致5人死亡超700人受伤
  • 甘肃省原副省长赵金云被开除公职,甘肃省委表态:坚决拥护党中央决定
  • 王毅会见乌兹别克斯坦外长赛义多夫
  • 秦洪看盘|短线热点降温,A股回落整固