小编Fou*_*SSI的帖子

在 Laravel 验证规则“存在”中使用模型关系

数据库架构

users
 -id
 -name
 -email
 ...

roles
 -id
 -name
Run Code Online (Sandbox Code Playgroud)

用户可能具有多个角色,反之亦然(我已经在模型中定义了关系)

数据透视表

role_user
 -id
 -user_id
 -role_id
Run Code Online (Sandbox Code Playgroud)

验证规则尝试制定:user_id 必须存在于用户表中并且角色 id = 4

//...
'user_id' => ['nullable', Rule::exists('users')->where(
                function ($query) { 
                    $query->whereHas('roles', 
                        function ($q) { 
                            $q->where('id', 4); 
                        }
                    );
                }
)], 
//...
Run Code Online (Sandbox Code Playgroud)

错误消息:“SQLSTATE[42S22]:未找到列:1054 'where 子句'中存在未知列'has'(SQL:选择 count(*) 作为来自 userswhere user_id= 0 和 ( has= 角色) 的聚合)”

php laravel eloquent laravel-validation

2
推荐指数
1
解决办法
8341
查看次数

Laravel Nova 条件验证:required_if + 存在规则

我正在尝试验证有关字段值'parent_code'的 attr'level'

这是我想要实现的目标:

'parent_code'仅当is != 0 时才需要'level'(这部分工作正常)

设置后,它也必须存在于表中'products''product_code':将使用的列名)

我当前的代码(无法正常工作)

产品资源类

public function fields(Request $request) {
        return [
            ID::make()->sortable(),

            Text::make('Product code', 'product_code')
                ->rules('required')
                ->creationRules('unique:products,product_code')
                ->updateRules('unique:products,product_code,{{resourceId}}')->hideFromIndex(),


            Text::make('Product short name', 'product_short_name')->onlyOnForms(), 


            Textarea::make('Product name', 'product_name')
                ->rules('required')
                ->sortable()
                ->onlyOnForms(),

            Text::make('Parent code', 'parent_code')
                ->rules(['required_if:level,2,4,6', 'exists:products,product_code'])
                ->hideFromIndex(), 

            Select::make('Level', 'level')->options([
                '0' => 'Sector level',
                '2' => 'H2',
                '4' => 'H4',
                '6' => 'H6',
            ])->rules('required')->sortable(),    

        ];
}
Run Code Online (Sandbox Code Playgroud)

创建产品表格

创建产品表单

感谢您的帮助。

php laravel laravel-validation laravel-nova

1
推荐指数
1
解决办法
5053
查看次数

Laravel 缓存标签的真实使用示例

根据 Laravel文档

缓存标签允许您标记缓存中的相关项目,然后刷新已分配给给定标签的所有缓存值。你可以通过传入一个有序的标签名称数组来访问一个标签缓存。例如,让我们访问标记缓存并将值放入缓存中:

Cache::tags(['people', 'artists'])->put('John', $john, $minutes);

Cache::tags(['people', 'authors'])->put('Anne', $anne, $minutes);
Run Code Online (Sandbox Code Playgroud)

它们有什么用?

laravel laravel-cache

1
推荐指数
1
解决办法
1322
查看次数