我很困惑如何在laravel中存储唯一值我想只存储单值transaction_id,那里不应该有多个值。请建议。
$payment = new Payment;
$payment->order_id = Input::get('orderid');
$payment->amount = (Input::get('base_amount', 0))?Input::get('base_amount'):Input::get('amount');
$payment->transaction_id = Input::get('transact');
how to check if this $payment->transaction_id already exists should not save more than one
i tried
$payment = Payment::where('transaction_id', '=', Input::get('transaction_id'))->first();
if ($payment === null) {
$payment->save();
}
答案 0 :(得分:0)
您可以使用请求验证:
public function rules()
{
return [
'transaction_id' => 'unique'
];
}
如果使用此方法,则应使用php artisan make:request MyRequest
命令创建新的Request类。然后将rules()
放入创建的MyRequest
中,然后您可以在RESTful控制器内部使用它来存储/更新操作(函数):
public function store(MyRequest $request)
{
dd($request->all());
}
https://laravel.com/docs/5.2/validation
此外,您可以在模型中使用验证:
private $rules = [
'transaction_id' => 'required'
];