0
votes

I have request for updating page. When adding new page request is like:

'title'=>'required|max:70|unique:pages',

but when I'm updating page title must be unique but need to check all other titles except one already entered. I searched Google for all possible solutions but nothing works.

I tried:

'title'=>"required|max:70|unique:pages, title,{$this->id}",
'title'=>'required|max:70|unique:pages, title,'.$this->id,
'title'=>'required|max:70|unique:pages, title,'.$this->input('id'),

rule is inside rules method

public function rules()
{
    return [
    'title'=>'required|max:70|unique:pages, title,'.$this->id,
    ...
    ]

I'm getting this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column ' title' in 'where clause' (SQL: select count(*) as aggregate from `pages` where ` title` = test and `id` <> )

In my mysql I have id (lowercase) column that is primary key and title column (also lowercase).

2

2 Answers

5
votes

You have a bug here:

public function rules()
{                                          This space is causing trouble
                                           | 
    return [
    'title'=>'required|max:70|unique:pages, title,'.$this->id,
    ...
    ]

Should be

public function rules()
{                                          Space removed
                                           | 
    return [
    'title'=>'required|max:70|unique:pages,title,'.$this->id,
    ...
    ]
1
votes
SQLSTATE[42S22]: Column not found: 1054 Unknown column ' title'

It seems that it's searching for a column named " title", it may be stupid but double check that the column name is sent without a space at the beggining.