douzhang2092 2015-12-14 16:41
浏览 66

Laravel 5.1中的复合键验证

I am trying the below query in Laravel 5.1

Select * from tblSubCategory 
Where SubCategoryID != 1 and CategoryID = 1 and SubCategory = 'test data';

Below is my code in Laravel

$v = \Validator::make($request->all(), [
    'SubCategory' => 'required|max:100|min:5|unique:tblsubcategory,SubCategory,'
        .''.$request->input('CategoryID')
        .',CategoryID,SubCategoryID,'.$request->input('SubCategoryID')

]);

Above expression is producing below query.

select count(*) as aggregate from `tblsubcategory` 
where `SubCategory` = 'under website1' and `CategoryID` <> 1 and `SubCategoryID` = 1

Unique Key

UNIQUE KEY `UK_tblSubCategory_SubCategory_CategoryID` (`CategoryID`,`SubCategory`);
  • 写回答

1条回答 默认 最新

  • duanliusong6395 2015-12-15 06:34
    关注

    Approach:1: Compatible with Laravel : 5.*

    On the command line:composer require felixkiss/uniquewith-validator:2.*

    Add the following to your providers array in config/app.php:

    'providers' => array(
        'Felixkiss\UniqueWithValidator\UniqueWithValidatorServiceProvider',
    ),
    
    $v = \Validator::make($request->all(), [
        'SubCategory' => 'required|max:100|min:5|unique_with:tblsubcategory,CategoryID',
        'CategoryID' => 'required',
    ]);
    

    Approach:2

    $v = \Validator::make($request->all(), [
        'SubCategory' => 'unique:tblsubcategory,SubCategory,'
                         .$request->input('SubCategoryID') 
                         .',SubCategoryID,CategoryID,'
                         .$request->input('CategoryID')
    ]);
    
    if ($v->fails()) {
        return \Redirect::back()
                    ->withErrors($v)
                    ->withInput();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)