1

I am experiencing an issue in my create function,

SQLSTATE[HY000]: General error: 1364 Field 'grandtotal' doesn't have a default value (HY000)

upon digging I learned that I need to remove the comma from the value, in my case, in grandtotal and cost they're both decimal.

from this question How to format to decimal number that include comma and dot in laravel?, the answer suggested this code.

$input = '10,000.1111';
$model->column = (float) str_replace(',', '', $input);
$model->save()

my question is how can I apply it to current code?

Here's my store function

$orders = Orders::create($request->only(
        'user_id',
        'status_id',
        'currency_id',
        'company_id',
        'purchase_no',
        'notes',
        'delivery_date',
        'grandtotal', '=>' , (float) str_replace(',', '', 'grandtotal'),
        'publish'
    ));

    $input = $request->all();

    for($i=0; $i<= count($input['quantity']); $i++) {

      if(empty($input['quantity'][$i]) || !is_numeric($input['quantity'][$i])) continue;

      $items = [ 
        'product_id' => $input['product_id'][$i],
        'product_code' => $input['product_code'][$i],
        'name' => $input['name'],
        // 'cost' => $input['cost'][$i],
        'cost' => (float) str_replace(',', '', $input['cost'][$i]),
        'quantity' => intval($input['quantity'][$i]),
      ];

      Order::create($items);
    }

    return redirect(route('orders.index'));

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
SleepWalker
  • 613
  • 1
  • 12
  • 39
  • What have you tried to debug the problem? If inserting is not possible, have you checked which **exact** value is used? – Nico Haase Feb 26 '19 at 11:21
  • You don't provide a value for `grandtotal` in your `create()` method, and there is no default value. The error you posted in your question has nothing to do with your comma. Though you should remove it, and you already have the code for that (at the top). – Qirel Feb 26 '19 at 11:21
  • you're just lazy to trying basically you just need to override $input['cost'][$i] Like $newcost = (float) str_replace(',', '', $input['cost'][$i]); and replace 'cost' => $newcost, and also for grand total its better you use accessor read some on the docs about accessor – Ghiffari Assamar Feb 26 '19 at 11:26
  • hi @Qirel thanks I add the screenshot of the issue, It has a 10,234 as value. Yes although I found the code, my real problem is how can I incorporate it to 'grandtotal' in $orders variable. :) – SleepWalker Feb 26 '19 at 11:33

1 Answers1

3

You just replace the marked line with following line

'cost' => (float) str_replace(',', '', $input['cost'][$i]),
Alpy
  • 759
  • 6
  • 9
  • Thank you @Alpy! can you show me also how to apply it to the 'grandtotal' in $orders? – SleepWalker Feb 26 '19 at 11:36
  • I apply it to 'grandtotal, 'grandtotal' =>(float) str_replace(',', '', $orders['grandtotal']),.. But I am having this error, Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')' – SleepWalker Feb 26 '19 at 17:17