12
votes

SQLSTATE[01000]: Warning: 1265 Data truncated for column 'pay_totals' at row 1

public function order(Request $req){
        $order = new Order;
        $order->pay_number = $req->checkout_number;
        $order->pay_totals = $req->checkout_total;
        $order->save();
        return redirect(route('pay'))->with('message','Sending infomation successfully');
    }

blade:

<input type="text" name="checkout_total" value="{{Cart::subTotal('0') }} ">

Helppp

3
The root cause of this error lies with MySQL, and as we can't see anything about your database table I don't think this question is answerable. Most likely, you are trying to store some data which is too wide to fit in the target column, and MySQL is warning you about this. - Tim Biegeleisen

3 Answers

21
votes

The problem is that column pay_totals can't store whatever you are getting from the input because is too big.

Possibles solutions

SQL: ALTER TABLE [orders] ALTER COLUMN [pay_totals] VARCHAR(MAX)

MYSQL: ALTER TABLE [orders] MODIFY COLUMN [pay_totals] VARCHAR(60000)

10
votes

Same error can be occurred due data type mismatching. As an example,if you are assigning an string to float, you will get this error. so be sure to check whether the data are in correct type. I got this error on symfony.

2
votes

Some times this error occurred due to inserting data into extra columns like ('created_at','updated_At') in pivot table. Below solution works for me.

Error Code

$users=User::create($input['data']);
foreach ($input['products'] as $product) {
    if (array_key_exists('special_price', $product)) {
        $userProducts[$product['id']] = ['special_price' => $product['special_price']];
    } else {
        $userProducts[$product['id']] = ['special_price' => ''];
    }
    $userProducts['created_at'] = Carbon::now()->toDateTimeString();
    $userProducts['updated_at'] = Carbon::now()->toDateTimeString();
}

//individual product with special price of each product
/*
Output before code correction

array (1 =>
array(
    'special_price' => '',
),
'created_at' => '2019-04-07 14:28:27',
'updated_at' => '2019-04-07 14:28:27',))
*/
$users->products()->sync($userProducts);

Code Correction

$users=User::create($input['data']);
foreach ($input['products'] as $product) {
    if (array_key_exists('special_price', $product)) {
        $userProducts[$product['id']] = ['special_price' => $product['special_price']];
    } else {
        $userProducts[$product['id']] = ['special_price' => ''];
    }
    // below lines are after code correction
    $userProducts[$product['id']]['created_at'] = Carbon::now()->toDateTimeString();
    $userProducts[$product['id']]['updated_at'] = Carbon::now()->toDateTimeString();
}

//individual product with special price of each product
/*
Output after code correction

array (1 =>
array(
    'special_price' => '',
    'created_at' => '2019-04-07 14:28:27',
    'updated_at' => '2019-04-07 14:28:27',
),)
*/
$users->products()->sync($userProducts);