Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david2000's avatar

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

I am trying to change the value of my fields matriculation and number_motorbke 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');

        }   

    }

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."

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();
}
0 likes
4 replies
jlrdw's avatar

Why here:

return redirect()->route('motorbikes.index')
        ->with('success', 'Update!')->withInput();

Are you needing ->withInput().

davidifranco's avatar

https://laravel.com/docs/6.x/validation#rule-unique

On update you should exclude the id being updated from unique rule.

From the docs:

Forcing A Unique Rule To Ignore A Given ID:

Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. You will probably want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address.

To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:

use Illuminate\Validation\Rule;

Validator::make($data, [ 'email' => [ 'required', Rule::unique('users')->ignore($user->id), ], ]);

munazzil's avatar

That error shows because of unique in validation unique:motorbikes,matriculation can you remove both validation and check,

       return [
        'matriculation' => 'required|string|max:15|min:6|unique:motorbikes,matriculation',
        'number_motorbike' => 'required|string|max:6|min:6|unique:motorbikes,number_motorbike'
    ];

and below one has same min and max value,

     'number_motorbike' => 'required|string|max:6|min:6|unique:motorbikes,number_motorbike'
david2000's avatar

Hello,

I have tried the regle "ignore" , but I think that I have a problem with the syntax ?

'matriculation' => 'required|string|max:15|min:6|unique:motorbikes->ignore($matriculation->id)' 

Please or to participate in this conversation.