mercredi 13 février 2019

Laravel 5.4 Form Request unique validation: how to access id?

I have this form request as follows:

<?php

namespace App\Http\Requests;

use App\Sociallink;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class SociallinkRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
          'seq' => 'required|unique:sociallinks,seq,' . $this->id . ',id',
          'social_name' => 'required|unique:sociallinks,social_name,' . $this->id . ',id',
          'cssclass' => 'required',
          'url' => 'nullable|active_url'
        ];
    }

I need the fields seq and social_name as unique. When I am trying to edit, this code is not working. I found that $this->id doesnt exist from dd($this). My URL is: http://prj.test/sociallink/2/edit. Many examples here make use of $this->id but I cant access the variable anywhere in my code as it seems to be non existent. When I replace $this->id with a physical id like 2 in this example then the validation works fine. But dynamically, how do I use the current row's id to do the unique validation?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire