分类目录归档:日志

Laravel 验证规则中的 exists 和 unique 应该怎么使用

exists:table,column

验证字段的值是否存在于数据库指定表的指定列中,如果不存在将会抛出异常,这里是关于 exists 规则的 详细说明

基本用法

验证 users 表中是否存在 email 等于指定的 email 值的记录

  • 'email' => 'exists:users'
  • Rule::exists('users')

自定义列名

验证 users 表的 email_address

  • 'email' => 'exists:users,email_address'
  • Rule::exists('users','email_address')

自定义数据库连接

验证指定数据库连接的 users 表

  • 'email' => 'exists:connection.users,email'

附加 WHERE 条件(可以有多个)

除了验证 email_address 还需要验证 role 等于 admin

  • 'email' => 'exists:users,email_address,role,admin'
  • Rule::exists('users','email_address')->where('role','admin')

! 表示不等于

  • 'email' => 'exists:users,email_address,role,!admin'
  • Rule::exists('users','email_address')->whereNot('role','admin')

NULL 在 MySQL 中是通过 IS 判断的,这里注意不用 IS

  • 'email' => 'exists:users,email,deleted_at,NULL'
  • Rule::exists('users','email_address')->whereNull('deleted_at')

对不等于 NULL 的情况不能使用 !,而是使用 NOT_NULL

  • 'email' => 'exists:users,email,deleted_at,NOT_NULL'
  • Rule::exists('users','email_address')->whereNotNull('deleted_at')

unique:table,column,except,idColumn

验证字段的值在数据库指定表的指定列中是否唯一,如果不唯一将会抛出异常,这里是关于 unique 规则的 详细说明

基本用法

验证指定的 email 值在 users 表的 email 中是否唯一

  • 'email' => 'unique:users '
  • Rule::unique('users')

自定义列名

验证 users 表的 email_address

  • 'email' => 'unique:users,email_address'
  • Rule::unique('users','email_address')

自定义数据库连接

验证指定数据库连接的 users 表

  • 'email' => 'unique:connection.users,email_address'

排除某条记录

排除 users 表中 id 等于 $userId 的记录,使用场景是用户修改 email 时,需要验证自身除外的其他用户是否已使用了该 email

  • 'email' => 'unique:users,email_address,'.$userId
  • Rule::unique('users','email_address')->ignore($userId)

排除时指定 primary_key

排除 users 表中 user_id 等于 $userId 的记录

  • 'email' => 'unique:users,email_address,'.$userId.',user_id'
  • Rule::unique('users','email_address')->ignore($userId,'user_id')

附加 WHERE 条件(和 exists 的 WHERE 条件相同)

由于排除是在 WHERE 条件之前的,如果指定了 WHERE 条件,但是没有指定排除,需要把第三个参数设为 NULL

  • 'email' => 'unique:users,email_address,NULL,id,role,admin'
  • Rule::unique('users','email_address')->where($role,'admin')

在Ubuntu 18.04上安装MySQL

使用 apt 或 tasksel 安装完 mysql-server 之后,默认无法登录,需要进行配置。

  1. 使用命令 mysql_secure_installation 并按提示操作,之后重启服务。
  2. 使用 sudo mysql -u root -p 登录,密码为空,或上一步设置的密码。
  3. 运行以下SQL: update mysql.user set plugin='mysql_native_password', authentication_string=password('root') where User='root'; ,退出,重启服务。

新密码是 root

mysql 8.0 无法登录的解决办法:

  1. 更改加密方式:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
  1. 更改密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
  1. 刷新:
mysql> FLUSH PRIVILEGES;

Laravel 5.4: Specified key was too long error

Laravel 5.4 made a change to the default database character set, and it’s now utf8mb4 which includes support for storing emojis. This only affects new applications and as long as you are running MySQL v5.7.7 and higher you do not need to do anything.

For those running MariaDB or older versions of MySQL you may hit this error when trying to run migrations:

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

After that everything should work as normal. If you enjoyed this post be sure and join the weekly Laravel newsletter and never miss out on new releases, framework tips, and new tutorials.

让 vmware 共享文件夹自动加载的办法

因为新电脑不能直接安装 linux ,只能使用 vmware 内安装虚拟机。安装 vmware 扩展工具,才能使用图形加速,共享目录等功能。

apt install open-vm-tools

设置了 共享文件夹 后,每次开机后都要连接共享,如果要实现自动加载,在 /etc/fstab 中添加以下代码:

.host:/share_name /mnt/hgfs/access fuse.vmhgfs-fuse allow_other,uid=1000,gid=1000,auto_unmount,defaults 0 0

share_name 是主机共享出来的目录名,/mnt/hgfs/access 是虚拟机内的访问路径。

在 Ubuntu 上配置和优化 Apache

你可以在 Apache 上做各种各样的配置,但是主要的和最常见的配置将在下面做出解释。

检查 Apache 是否正在运行

默认情况下,Apache 设置为在机器启动时自动启动,因此你不必手动启用它。你可以使用以下命令检查它是否正在运行以及其他相关信息:

systemctl status apache2

check if apache is running
check if apache is running

并且你可以检查你正在使用的版本:

apachectl -V

一种更简单的检查方法时访问服务器的 IP 地址,如果你得到默认的 Apache 页面,那么一切都正常。

继续阅读

Laravel 中 validation 验证应该怎么设置中文信息提示

首先复制 resources\lang\en 目录下边的 validation.php到新建的文件夹zh-cn中,用下面代码替换之前的代码,在 attributes 中设置字段,最后设置config下的app.php中修改 'locale' => 'zh-cn'

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines contain the default error messages used by
    | the validator class. Some of these rules have multiple versions such
    | as the size rules. Feel free to tweak each of these messages here.
    |
    */

    'accepted'             => ':attribute必须接受',
    'active_url'           => ':attribute必须是一个合法的 URL',
    'after'                => ':attribute 必须是 :date 之后的一个日期',
    'after_or_equal'       => ':attribute 必须是 :date 之后或相同的一个日期',
    'alpha'                => ':attribute只能包含字母',
    'alpha_dash'           => ':attribute只能包含字母、数字、中划线或下划线',
    'alpha_num'            => ':attribute只能包含字母和数字',
    'array'                => ':attribute必须是一个数组',
    'before'               => ':attribute 必须是 :date 之前的一个日期',
    'before_or_equal'      => ':attribute 必须是 :date 之前或相同的一个日期',
    'between'              => [
        'numeric' => ':attribute 必须在 :min 到 :max 之间',
        'file'    => ':attribute 必须在 :min 到 :max KB 之间',
        'string'  => ':attribute 必须在 :min 到 :max 个字符之间',
        'array'   => ':attribute 必须在 :min 到 :max 项之间',
    ],
    'boolean'              => ':attribute 字符必须是 true 或 false',
    'confirmed'            => ':attribute 二次确认不匹配',
    'date'                 => ':attribute 必须是一个合法的日期',
    'date_format'          => ':attribute 与给定的格式 :format 不符合',
    'different'            => ':attribute 必须不同于 :other',
    'digits'               => ':attribute必须是 :digits 位.',
    'digits_between'       => ':attribute 必须在 :min 和 :max 位之间',
    'dimensions'           => ':attribute具有无效的图片尺寸',
    'distinct'             => ':attribute字段具有重复值',
    'email'                => ':attribute必须是一个合法的电子邮件地址',
    'exists'               => '选定的 :attribute 是无效的.',
    'file'                 => ':attribute必须是一个文件',
    'filled'               => ':attribute的字段是必填的',
    'image'                => ':attribute必须是 jpeg, png, bmp 或者 gif 格式的图片',
    'in'                   => '选定的 :attribute 是无效的',
    'in_array'             => ':attribute 字段不存在于 :other',
    'integer'              => ':attribute 必须是个整数',
    'ip'                   => ':attribute必须是一个合法的 IP 地址。',
    'json'                 => ':attribute必须是一个合法的 JSON 字符串',
    'max'                  => [
        'numeric' => ':attribute 的最大长度为 :max 位',
        'file'    => ':attribute 的最大为 :max',
        'string'  => ':attribute 的最大长度为 :max 字符',
        'array'   => ':attribute 的最大个数为 :max 个.',
    ],
    'mimes'                => ':attribute 的文件类型必须是 :values',
    'min'                  => [
        'numeric' => ':attribute 的最小长度为 :min 位',
        'file'    => ':attribute 大小至少为 :min KB',
        'string'  => ':attribute 的最小长度为 :min 字符',
        'array'   => ':attribute 至少有 :min 项',
    ],
    'not_in'               => '选定的 :attribute 是无效的',
    'numeric'              => ':attribute 必须是数字',
    'present'              => ':attribute 字段必须存在',
    'regex'                => ':attribute 格式是无效的',
    'required'             => ':attribute 字段是必须的',
    'required_if'          => ':attribute 字段是必须的当 :other 是 :value',
    'required_unless'      => ':attribute 字段是必须的,除非 :other 是在 :values 中',
    'required_with'        => ':attribute 字段是必须的当 :values 是存在的',
    'required_with_all'    => ':attribute 字段是必须的当 :values 是存在的',
    'required_without'     => ':attribute 字段是必须的当 :values 是不存在的',
    'required_without_all' => ':attribute 字段是必须的当 没有一个 :values 是存在的',
    'same'                 => ':attribute和:other必须匹配',
    'size'                 => [
        'numeric' => ':attribute 必须是 :size 位',
        'file'    => ':attribute 必须是 :size KB',
        'string'  => ':attribute 必须是 :size 个字符',
        'array'   => ':attribute 必须包括 :size 项',
    ],
    'string'               => ':attribute 必须是一个字符串',
    'timezone'             => ':attribute 必须是个有效的时区.',
    'unique'               => ':attribute 已存在',
    'url'                  => ':attribute 无效的格式',

    /*
    |--------------------------------------------------------------------------
    | Custom Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | Here you may specify custom validation messages for attributes using the
    | convention "attribute.rule" to name the lines. This makes it quick to
    | specify a specific custom language line for a given attribute rule.
    |
    */

    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Custom Validation Attributes
    |--------------------------------------------------------------------------
    |
    | The following language lines are used to swap attribute place-holders
    | with something more reader friendly such as E-Mail Address instead
    | of "email". This simply helps us make messages a little cleaner.
    |
    */

    'attributes' => [
        'username'      => '帐号',
        'pwd'           => '密码',
        'phone'         => '手机',
    ],

];

js中判断对象是否为空的方法

方法一

JSON.stringify(arr) === '[]'
arr.length === 0
+arr === 0

上面三种判断数组为空的方法虽然大多数情况下都可以用,但依然存在bug,比如令 arr[-1] = '' 时,数组不为空,但三者都返回true,所以如果业务代码中一旦遇到这种bug,就需要来个函数了:

var arr = []
arr[-1] = ''
function isEmptyObject(e) {
    var t;
    for (t in e)
        return false;
    return true;
}
isEmptyObject(arr);

继续阅读