在 laravel 中更新数据时显示错误的唯一键验证

Posted

技术标签:

【中文标题】在 laravel 中更新数据时显示错误的唯一键验证【英文标题】:Unique key validation showing error while updating data in laravel 【发布时间】:2015-01-06 14:57:47 【问题描述】:

您好,我正在使用 laravel 创建一个应用程序,我有一个模型,其中asset_number 字段是唯一的,验证规则如下

型号

protected $rules = array
        (

            'asset_number'              => 'required|alpha_dash|min:2|max:12|unique:asset_details,asset_number,id',
            'asset_name'                => 'alpha_space',
            'asset_type_id'             => 'required',
            'shift'                     => 'required',

        );

这是我用于创建和编辑的控制器代码

控制器

public function postCreate()
    

        // get the POST data
        $new = Input::all();

        // create a new Assetdetail instance
        $assetdetail = new Assetdetail();

        // attempt validation
        if ($assetdetail->validate($new))
        

            // Save the location data
            $assetdetail ->asset_number             = e(Input::get('asset_number'));
            $assetdetail ->asset_name               = e(Input::get('asset_name'));
            $assetdetail ->asset_type_id            = e(Input::get('asset_type_id'));
            $assetdetail ->shift                    = e(Input::get('shift'));

            // Was the asset created?
            if($assetdetail ->save())
            
                // Redirect to the new location  page
                return Redirect::to("admin/settings/assetdetails")->with('success', Lang::get('admin/assetdetails/message.create.success'));
            
        
        else
        
            // failure
            $errors = $assetdetail->errors();
            return Redirect::back()->withInput()->withErrors($errors);
        

        // Redirect to the location create page
        return Redirect::to('admin/settings/assetdetails/create')->with('error', Lang::get('admin/assetdetails/message.create.error'));

    


    public function getEdit($assetdetailId = null)
    
        // Check if the location exists
        if (is_null($assetdetail = Assetdetail::find($assetdetailId)))
        
            // Redirect to the blogs management page
            return Redirect::to('admin/settings/assetdetails')->with('error', Lang::get('admin/assetdetails/message.does_not_exist'));
        

        //$assetlife = DB::table('asset_details')->where('id', '=', $assetdetailId)->lists('asset_life');
        $location_list = array('' => '') + Location::lists('name', 'id');
        $assettype_list = array('' => '') + Assettype::lists('asset_type', 'id');
        $assignTo_list = array('' => 'Select a User') + User::select(DB::raw('CONCAT(first_name, " ", last_name) AS full_name'), 'id') ->lists('full_name', 'id');

        $assetdetail_options = array('' => 'Top Level') + DB::table('asset_details')->where('id', '!=', $assetdetailId)->lists('asset_number', 'id');

        return View::make('backend/assetdetails/edit', compact('assetdetail'))->with('assetdetail_options',$assetdetail_options)->with('location_list',$location_list)->with('assettype_list',$assettype_list)->with('assignTo_list',$assignTo_list);

    


    public function postEdit($assetdetailId = null)
    
        // Check if the location exists
        if (is_null($assetdetail = Assetdetail::find($assetdetailId)))
        
            // Redirect to the blogs management page
            return Redirect::to('admin/settings/assetdetails')->with('error', Lang::get('admin/assetdetails/message.does_not_exist'));
        

        $new = Input::all();

        /*if ($assetdetail ->asset_number   == Input::old('asset_number') && 
        
          $assetdetail ->asset_number = e(Input::get('asset_number'));
        */

        if ($assetdetail->validate($new))
        

            // Update the asset data
            $assetdetail ->asset_number             = e(Input::get('asset_number'));
            $assetdetail ->asset_name               = e(Input::get('asset_name'));
            $assetdetail ->asset_type_id            = e(Input::get('asset_type_id'));
            $assetdetail ->shift                    = e(Input::get('shift'));


            // Was the asset created?
            if($assetdetail->save())
            
                // Redirect to the saved location page
                return Redirect::to("admin/settings/assetdetails/$assetdetailId/edit")->with('success', Lang::get('admin/assetdetails/message.update.success'));
            
        

            else
            
            // failure
            $errors = $assetdetail->errors();
            return Redirect::back()->withInput()->withErrors($errors);
            


        // Redirect to the asset management page with error
        return Redirect::to("admin/settings/assetdetails/$assetdetailId/edit")->with('error', Lang::get('admin/assetdetails/message.update.error'));

    

我的Route.php

Route::post('create', array('as' => 'savenew/assetdetail','uses' => 'Controllers\Admin\AssetdetailsController@postCreate'));
            Route::get('assetdetailId/edit', array('as' => 'update/assetdetail', 'uses' => 'Controllers\Admin\AssetdetailsController@getEdit'));
            Route::post('assetdetailId/edit', 'Controllers\Admin\AssetdetailsController@postEdit');

现在的问题是,每当我尝试更新表单时,我的 assets_number 字段都会抛出 错误: asset_number 已被占用。如何在我的表单中传递 id控制器在更新我的表单时,只有在使用已经存在的asset_number时才会引发错误,而不是在使用当前asset_number编辑表单时引发错误。

我尝试了以下没有运气

型号

'asset_number' => 'mobileNumber' => 'required|min:5|numeric|unique:asset_details,asset_number,' . $id

控制器

Assetdetail::$rules['mobileNumber'] = 'required|min:5|numeric|unique:asset_details,asset_number,' . $id

【问题讨论】:

@Kyslik 用粗体字更新了我的问题,请看一下 你好,请看这个答案***.com/a/24205849/1564365 @Kyslik 我试过了,但仍然存在同样的错误......请提供任何其他解决方案 【参考方案1】:

嗨,我自己终于找到了一个可行的解决方案,这是我所做的,请看一下

控制器

class AssetdetailsController extends AdminController 
 

     protected $validationRules = array
        (   
            'asset_number'              => 'required|alpha_dash|min:2|max:12|unique:asset_details,asset_number',
            'asset_name'                => 'alpha_space',
            'asset_type_id'             => 'required',
            'shift'                     => 'required',
        );

    public function postCreate()
    

        $validator = Validator::make(Input::all(), $this->validationRules);

        // If validation fails, we'll exit the operation now.
        if ($validator->fails())
        
            // Ooops.. something went wrong
            return Redirect::back()->withInput()->withErrors($validator);
        

        // attempt validation
        else
        

            $assetdetail ->asset_number             = e(Input::get('asset_number'));
            $assetdetail ->asset_name               = e(Input::get('asset_name'));
            $assetdetail ->asset_type_id            = e(Input::get('asset_type_id'));
            $assetdetail ->shift                    = e(Input::get('shift'));

            // Was the asset created?
            if($assetdetail ->save())
            
                // Redirect to the new location  page
                return Redirect::to("admin/settings/assetdetails")->with('success', Lang::get('admin/assetdetails/message.create.success'));
            
        


        // Redirect to the location create page
        return Redirect::to('admin/settings/assetdetails/create')->with('error', Lang::get('admin/assetdetails/message.create.error'));

    

    public function postEdit($assetdetailId = null)
    
        // Check if the location exists
        if (is_null($assetdetail = Assetdetail::find($assetdetailId)))
        
            // Redirect to the blogs management page
            return Redirect::to('admin/settings/assetdetails')->with('error', Lang::get('admin/assetdetails/message.assetdetail_not_exist'));
        

        $this->validationRules['asset_number'] = "required|alpha_dash|min:2|max:12|unique:asset_details,asset_number,$assetdetail->asset_number,asset_number";

        $validator = Validator::make(Input::all(), $this->validationRules);


        // If validation fails, we'll exit the operation now.
        if ($validator->fails())
        
            // Ooops.. something went wrong
            return Redirect::back()->withInput()->withErrors($validator);
        

        else
        
            $assetdetail ->asset_number             = e(Input::get('asset_number'));
            $assetdetail ->asset_name               = e(Input::get('asset_name'));
            $assetdetail ->asset_type_id            = e(Input::get('asset_type_id'));
            $assetdetail ->shift                    = e(Input::get('shift'));

            if($assetdetail->save())
            
                // Redirect to the saved location page
                return Redirect::to("admin/settings/assetdetails/$assetdetailId/edit")->with('success', Lang::get('admin/assetdetails/message.update.success'));
            
        

        // Redirect to the asset management page with error
        return Redirect::to("admin/settings/assetdetails/$assetdetailId/edit")->with('error', Lang::get('admin/assetdetails/message.update.error'));

    

【讨论】:

以上是关于在 laravel 中更新数据时显示错误的唯一键验证的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5刀片在出现错误而不是抛出异常时显示空白页面

当我尝试在 laravel 中添加 vue js 时显示错误 -> 错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin'

在 Laravel + Vue 中页面加载时显示所有数据

laravel post提交数据时显示异常

尝试使用laravel在excel中导出大数据时显示超时

Flutter FutureBuilder 在调用 Rest API(Backend Laravel)时显示数据然后消失