mardi 21 juin 2016

Laravel 5 request validate slug upon save

Hi I am trying to validate a slug, so that when you enter a new entry if the slug is taken you get an error, yet when you do the same upon update you get the same error unless its the current record.

So heres my code:

/**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->method())
        {
            case 'GET':
            case 'DELETE':
            {
                return [];
            }
            case 'POST':
            {
                return [
                    'tenant_id'       => "required",
                    'assessment_name' => 'required',
                    'slug'            => 'required|unique:assessments,slug'
                ];
            }
            case 'PUT':
            case 'PATCH':
            {
                return [
                    'tenant_id'       => "required",
                    'assessment_name' => 'required',
                    'slug'            => "required|unique:assessments,slug,{$this->assessment->id}"
                ];
            }
            default:break;
        }

    }

Now $this->assessment->id gives me an ID of 7 in my test case which is a valid record. However, upon submitting the form i get

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'assessments' at line 1 (SQL: assessments)

If I remove the $this->assessment->id it submits but always states that the slug is a duplicate field.

What am I doing wrong here?

EDIT: The database table in question is called assessments.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire