mardi 15 octobre 2019

Function rules(), store() and update() in Laravel

I am trying to change the value of my fields matriculation and number_motorbike but visibly the values exist already. I have a problem of validation.

In my function rules() I have this: (no problem)

public function rules()
    {
        return [
            'matriculation' => 'required|string|max:15|min:6|unique:motorbikes,matriculation',
            'number_motorbike' => 'required|string|max:6|min:6|unique:motorbikes,number_motorbike'
        ];
    }

In my function store() I have this: (no problem)

ublic function store(MotoRequest $request)
    {


       $exists = Motorbike::where('matriculation', $request->get('matriculation'))->where('number_motorbike', $request->get('number_motorbike'))->count();

       if (!$exists){
            Motorbike::create($request->all());
            return redirect()->route('motorbikes.index')
                ->with('success', 'new data created successfully');
        }

        else{
            return redirect()->route('motorbikes.index')
                ->with('error', 'duplicate');

        }   

    }

enter image description here

My problem is in my function update(), when I want for example change only the value of my field matriculation , I get an error message "The number motorbike has already been taken."

enter image description here

Here my function update() but I think the problem is in my function rules()?

public function edit($id)
    {
        $motorbikes = Motorbike::find($id);
        return view('admin.motorbikes.edit', compact('motorbikes'));
    }

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(MotoRequest $request, $id)
{

    $motorbikes = Motorbike::find($id);
    $motorbikes->matriculation = $request->get('matriculation');
    $motorbikes->number_motorbike = $request->get('number_motorbike');
    $motorbikes->save();
    return redirect()->route('motorbikes.index')
        ->with('success', 'Update!')->withInput();
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire