为什么在store()会议中显示错误?

时间:2018-07-25 19:32:45

标签: laravel

我有此代码可以存储会议。有一个表单字段供用户选择可用的付款方式。表单字段如下:

<div class="form-group">
    <label for="inputName">
       Select the payment methods</label>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="payment_mb" id="payment_mb">
        <label class="form-check-label" for="exampleRadios1">
            MB
        </label>
    </div>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="payment_c" id="payment_c">
        <label class="form-check-label" for="exampleRadios1">
            Credit Card
        </label>
    </div>
</div>

要存储的代码如下。

但是显示错误:

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 
'on' for column 'payment_mb' at row 1 (SQL: 
 insert into `conferences` (`name`, `organizer_id`, 
 `organizer_name`, `organizer_email`, `invoice_entity`, 
 `payment_mb`, `updated_at`, `created_at`) 
 values (a, 2, John W, emailtest@test.com, 1,
 on, 2018-07-25 19:17:26, 2018-07-25 19:17:26))

你知道为什么吗?

public function store(Request $request)
    {

        $conference = Conference::create([
            'name' => $request->name,
            'city' => $request->city,
            'organizer_id' => Auth::id(),
            'organizer_name' => $request->organizer_name,
            'organizer_email' => $request->organizer_email,
            'payment_mb' =>$request->payment_mb ? : 0,
            'payment_c'  => $request->payment_c ? : 0
        ]);

        $conference->categories()->attach($request->categories);

        $conference->save();

        Session::flash('success', 'conference created.');
    }

$ request-> all()显示如下:

array:25 [▼
  "_token" => ""
  "name" => "test"
  "organizer_name" => "John W"
  "organizer_email" => "..."
  "payment_mb" => "on"
  "payment_c" => "on"
  "invoice_issuer" => "1"
]

2 个答案:

答案 0 :(得分:0)

您确定这是正确的商店代码吗? 即使已在您的代码中设置了payment_c,它也不会出现在sql语句中。

答案 1 :(得分:0)

on不会是int的值。您的请求的该字段值为'on'

'payment_mb' =>$request->payment_mb ? : 0,

您可以尝试:

'payment_mb' => (int) boolval($request->payment_mb),

甚至只是:

'payment_mb' => (bool) $request->payment_mb,

PHP Docs - boolval

您实际上还可以将复选框的值设置为'1'或类似的值。

一些验证可能会很好。

更新

我认为我正在寻找filter_var

filter_var($thatInput, FILTER_VALIDATE_BOOLEAN);

// 'on' => true
// null => false
// 'off' => false