0

I have two column 'book_name' & 'writer' in 'books' table. When new data insert i want to check the same book and writer not will be added. But i dont understand how to do this. here is my code.

public function rules()
{
    return [
        'book_name' => 'required|unique:books,book_name,' . $this->id,
        'writer' => 'required|unique:books,writer,' . $this->id,
    ];
}
mahbub
  • 103
  • 1
  • 3
  • 13

2 Answers2

0

According to laravel documentation, unique rule is applied by defining the rule, stating the table and column name e.g.

unique:table,column

alternativelly if you're using eloquent models it can be

unique:path\to\model,column

The . $this->id constructor should not be needed.

SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
Matt
  • 26
  • 3
0
public function rules()
{
    return [
        'book_name' => [
            'required',
            Rule::unique('books')->where(fn ($query) => $query->where('writer', request('writer')))
                ->ignore($this->id)
        ],

        'writer' => 'required',
    ];
}
mahbub
  • 103
  • 1
  • 3
  • 13