假设我有类别表,并且我已经使用了软删除。现在我第一次添加了一个类别“测试”后,我删除了这个类别,所以它会更新我的deleted_at
列从数据库。Laravel Soft删除唯一列名称
现在,当我再次尝试添加名为“Test”的类别时,它正在说我已经拍摄了该名称。我已经尝试过发布Here的规则。
但它不工作。我在模型中使用了特质。以下是我的模型代码。
<?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();
}
}
据我的了解laravel使用deleted_at列软删除,但数据库(MySQL的)不知道,所有它知道这是datetime列,所以,当你犯了一个列独特数据库遵循规则,保持其独特... ,我认为这将帮助你http://stackoverflow.com/questions/17458681/laravel-4-how-use-use-a-unique-validation-rule-unique -columns-with-soft-delet?lq = 1 – NULL 2014-10-17 11:47:12