假設我有類別表,並且我已經使用了軟刪除。現在我第一次添加了一個類別「測試」後,我刪除了這個類別,所以它會更新我的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