假设我有类别表,并且我在上面使用了软删除。现在我第一次添加了一个类别“测试”,之后我删除了这个类别,所以它会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();
}
}