mercredi 27 janvier 2016

Laravel 5.1 - Update name for current entry, must be unique to current User ID

I've got this code below in my GeckoRequest file. It works for the most part, however if I try to update an entry and keep the name as it is currently it fails saying that the name needs to be unique... Which it is as no other entries have this name apart from the current entry loaded in the edit form.

What is the best thing for me to do to check that the name is unique for the current user but also allow me to update the same entry without changing the name?

public function rules()
{
    switch($this->method())
    {
        case 'GET':
        case 'DELETE':
        {
            return [];
        }
        case 'POST':
        {
            return [
                'morph' => 'required',
                'sex' => 'required',
                'genetics' => 'required',
                'name' => "required|unique:geckos,name,NULL,id,user_id," . \Auth::user()->id
            ];
        }
        case 'PUT':
        case 'PATCH':
        {
            return [
                'morph' => 'required',
                'sex' => 'required',
                'genetics' => 'required',
                'name' => "required|unique:geckos,name,NULL,id,user_id," . \Auth::user()->id
            ];
        }
        default:break;
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire