mardi 7 janvier 2020

custom validation using laravel 5.8

****good day, i trying to update brand data using laravel 5.8 and i make custom validation function to validate name of brand but my problem is when i make validation update not work and i get output message "ops name exits before " but i need to update this validation function to link brand id with brand name to make update without showing validation

thanks in advance this is my function** here is my code** my controller

public function update(Request $request, $id)
{
    //prepare data for validation
    request()->validate([
        'name'  => [
            'required',
            'min:2', // validate english name is exist before
            function ($attribute, $value, $fail) {
                $englishname=Brand::where(['name'=>$value,'deleted'=>1 ])->first();
                if(false !=$englishname) {
                    $fail('Opps '.$attribute.' Is Exist Before.');
                }
            },
        ],
        'keywords' => 'required|min:2',
        'ar_name'  => [
            'required',
            'min:2',// validate english name is exist before
            function ($attribute, $value, $fail) {
                $arname=Brand::where(['ar_name'=>$value,'deleted'=>1])->first();
                if(false !=$arname) {
                    $fail('Opps '.$attribute.' Is Exist Before.');
                }
            },
        ],
        'ar_keywords' => 'nullable',
        'status' => 'required|integer',
    ],[],[
        "name"=>"Brand Name",
        'keywords' => 'Brand KeyWords',
        'ar_name' => 'اسم الماركة',
        'ar_keywords' => 'الكلمات الدليلية',

    ]);
    // start pass data to model
    $branddata=array(
        'name'          =>$request->name,
        'keywords'      =>$request->keywords,
        'ar_name'       =>$request->ar_name,
        'ar_keywords'   =>$request->ar_keywords,
        'last_updated_by'=>auth()->user()->id,
        'status'        =>$request->status,
    );

    //start update data
    $updateddata=Brand::where(['id'=>$id,'deleted'=>1])->update($branddata);
    if (false !==Brand::create($updateddata))
    {
        return redirect(route("brand.edit"))->with("messageSuccess","Brand Updated  Successfully");

    }else{
        return redirect(route("brand.edit"))->with("messageSuccess","Brand Updated Successfully");

    }


}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire