Laravel 8 xác thực hai cột duy nhất

Nếu bạn đang làm việc với Laravel, Nova là một tùy chọn CMS khá chắc chắn. Nó cung cấp rất nhiều quyền kiểm soát và bạn có thể tận dụng tất cả sức mạnh mà Laravel và cộng đồng của nó cung cấp. Giải pháp trong bài viết này cũng không ngoại lệ. bằng cách kết hợp các khái niệm Laravel tích hợp, chúng ta có thể nhanh chóng giải quyết vấn đề của mình

Xác thực tính duy nhất trong Nova không có vấn đề gì. Khi bạn cài đặt Nova, bạn sẽ nhận được Tài nguyên người dùng miễn phí, tài nguyên này đã bao gồm các quy tắc xác thực phù hợp

Text::make['Email']
    ->sortable[]
    ->rules['required', 'email', 'max:254']
    ->creationRules['unique:users,email']
    ->updateRules['unique:users,email,{{resourceId}}'],

Điều này sẽ tạo ra một thông báo lỗi gọn gàng bất cứ khi nào một địa chỉ email trùng lặp được gửi

Nova không gặp vấn đề gì khi xác thực các giá trị đơn duy nhất

Tuy nhiên, bạn sẽ làm gì khi khóa duy nhất của bạn được tạo thành từ nhiều cột?

Xử lý nhiều cột

Để thiết lập giai đoạn, hãy tưởng tượng một tài nguyên Trang. Mỗi trang có một con sên và một parent_id. Mỗi URL của trang được xác định bởi sên của nó, đứng trước là sên của trang mẹ

Nói cách khác, trang “Phát triển web”, là trang con của “Dịch vụ”, trang con của “Giới thiệu về chúng tôi”, sẽ kết thúc bằng URL.

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];
5

Từ các quy tắc này, chúng ta có thể rút ra một ràng buộc tự nhiên. Slug cần phải là duy nhất ở cấp độ cha mẹ của nó. Một con sên có thể xuất hiện nhiều lần, miễn là các trang mẹ của chúng khác nhau

Trước tiên, hãy tạo một di chuyển để tạo đúng bảng

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];

Sau đó, chúng tôi cần hai trường trên tài nguyên, một cho sên, một cho cha mẹ

Slug::make['slug']
    ->from['title']
    ->default[''],

BelongsTo::make[
    'parent',
    'parent',
    static::class
]
    ->nullable[]
    ->display['title'],

Lưu ý rằng chúng tôi không thể thêm các quy tắc duy nhất vào các trường này vì chúng tôi không thể gói gọn quy tắc đầy đủ

Mặc dù Laravel hỗ trợ xác thực duy nhất bằng cách sử dụng nhiều cột, nhưng chúng tôi không có cách nào thiết lập giá trị bổ sung trong ngữ cảnh của Tài nguyên Nova

Một ví dụ về một quy tắc sẽ hoạt động trên lý thuyết là đây

Slug::make['slug']
    ->from['title']
    ->default['']
    ->rules['unique:pages,slug,{{resourceId}},id,parent_id,???']

Nhưng như bạn có thể thấy, chúng tôi không thể cung cấp giá trị cho

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];
0, vì giá trị đó sẽ không tồn tại cho đến sau khi biểu mẫu được gửi

Sử dụng hook afterValidation của Nova

Trong lớp tài nguyên Nova, bạn có thể triển khai phương thức

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];
1 để thực hiện xác thực bổ sung. Nơi hoàn hảo để thêm logic miền tùy chỉnh của bạn

protected static function afterValidation[
    NovaRequest $request,
    $validator
] {
    if [$this->somethingElseIsInvalid[]] {
        $validator->errors[]->add[
            'field',
            'Something is wrong with this field!'
        ];
    }
}

Như bạn có thể thấy, bạn có thể triển khai điều này theo bất kỳ cách nào bạn muốn và thêm các lỗi tùy chỉnh vào phiên bản

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];
2 trong trường hợp không thành công

Đây là giải pháp đầy đủ nếu bạn chỉ ở đây để sao chép và dán

________số 8

Hãy để tôi phá vỡ nó để bạn biết những gì đang xảy ra

$parentId = $request->post['parent'];
$unique = Rule::unique['pages', 'slug']->where[
    'parent_id',
    $parentId
];

Đầu tiên, chúng tôi lấy giá trị cho

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];
3 từ postdata của yêu cầu. Đây là giá trị vừa được gửi trong biểu mẫu

Từ đây, chúng tôi tạo một Quy tắc duy nhất mới. Mệnh đề

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];
4 sẽ được thêm vào truy vấn

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];
2

Điều này xử lý trường hợp cập nhật. Nếu chúng ta bỏ qua những dòng này, thì không tài nguyên nào có thể được cập nhật lại với cùng một con sên, vì quy tắc sẽ luôn tìm thấy một bản ghi phù hợp. chính nó

Khi đã có quy tắc, chúng tôi có thể tạo phiên bản

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];
2 mới và xác thực sên bằng quy tắc tùy chỉnh của chúng tôi

Schema::create['pages', function [Blueprint $table] {
    $table->id[];
    $table->string['title'];
    $table->string['slug']->nullable[];
}];

// Adding a foreign key to the same table
// while creating it is not possible.
Schema::table['pages', function [Blueprint $table] {
    $table
        ->foreignId['parent_id']
        ->nullable[]
        ->references['id']
        ->on['pages'];

    // Create compound unique index on parent_id and slug:
    // slugs can only exist once at the same level.
    $table->unique[['parent_id', 'slug']];
}];
4

Đó là nó. Sử dụng một vài khái niệm mạnh mẽ, bản địa, Laravel, chúng tôi có thể mở rộng các tính năng tích hợp sẵn của Nova để phù hợp với các quy tắc miền của riêng chúng tôi

💡 Bạn đang tìm cách xác thực nhiều cột duy nhất bên ngoài ngữ cảnh Nova?

Chủ Đề