* @created 2022-08-26 14:21:32 * @modified 2022-08-26 14:21:32 */ namespace Beike\Shop\Http\Requests; use Beike\Models\ProductSku; use Illuminate\Foundation\Http\FormRequest; class CartRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize(): bool { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { $skuId = (int) $this->get('sku_id'); return [ 'sku_id' => 'required|int', 'quantity' => ['required', 'int', function ($attribute, $value, $fail) use ($skuId) { $sku = ProductSku::query()->where('id', $skuId)->first(); $skuQuantity = $sku->quantity; if ($value > $skuQuantity) { $fail(trans('cart.stock_out')); } if($sku->product->price_setting == 'num' && $value < $sku->product->numprices[0]->num){ $fail(trans('shop/products.quantity_error')); } }], 'buy_now' => 'bool', ]; } public function attributes() { return [ 'sku_id' => trans('cart.sku_id'), 'quantity' => trans('cart.quantity'), ]; } }