カテゴリ テーブルがあり、論理的な削除を使用したとします。初めてカテゴリ「テスト」を追加した後、このカテゴリを削除したためdeleted_at
、データベースから列が更新されます。
ここで、「Test」という名前のカテゴリを再度追加しようとすると、この名前が使用されていると表示されます。ここに掲載されているルールを試してみました。
しかし、それは機能していません。モデルで特性を使用しました。以下は私のモデルコードです。
<?php
namespace App\Models;
use Illuminate\Support\Facades\Validator as Validator;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Category extends \Eloquent {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
/**
* Guarded fields which are not mass fillable
*
* @var array
*/
protected $guarded = array('id');
/**
* Name of the table used
*
* @var string
*/
protected $table = 'travel_categories';
/**
* Validating input.
*
* @param array $input
* @return mixed (boolean | array)
*/
public static function validate($input, $id = null) {
$rules = array(
'name' => array('required','min:2','max:100','regex:/[a-zA-z ]/','unique:travel_categories,name,NULL,id,deleted_at,NULL'),
'description' => 'Required|Min:2',
'image' => 'image'
);
if ($id) {
$rules['name'] = 'Required|Between:3,64|Unique:travel_categories,name,'.$id;
}
$validation = Validator::make($input,$rules);
return ($validation->passes())?true : $validation->messages();
}
}