Skip to content
Advertisement

Error converting nvarchar into int… being a number? – Laravel, SQL

I am creating a json to save 2 values of a form that have the same name (data) in order to when 2 rows are saved in the table instead of just one at the same time.

This structure is created like this and cannot be edited.

In principle it works, but in the end it gives me an error that I am not very convinced that it is a Laravel problem or is just SQL, so I do not know where to go.

I have tried to save the value “1” twice to see what it came out, and the following error has come out:

SQLSTATE[22018]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion error when converting the value nvarchar ‘[“1″,”1”]’ to data type int. (SQL: insert into [table_data] ([data]) values ([“1″,”1”]))

The DB and the project itself cannot be simpler, so I don’t know where to go, any idea?.

DB Structure

  • ID (INT, Primary Key, AUTOINCREMENT)
  • Data (INT)

DataController.php

namespace AppHttpControllersdata;

use AppData;
use AppHttpControllersController;
use IlluminateHttpRequest;

use AppHttpRequestsStoreDataPost;

class DataController extends Controller
{
    public function create()
    {
        return view('data', ['Data' => new Data()]);
    }

    public function store(StoreDataPost $request)
    {
        $json = json_encode($request->get('data')); 
        Data::create(['data' => $json]);
    }
}

Data.php

namespace App;

use IlluminateDatabaseEloquentModel;

class Data extends Model
{
    protected $table = 'table_data';

    protected $primaryKey = 'ID';

    protected $fillable = [
        'data'
    ];

    public $timestamps = false;

    public function getDataAttribute()
    {
       return json_decode($this->data);
    }
}

StoreDataPost.php

namespace AppHttpRequests;

use IlluminateFoundationHttpFormRequest;

class StoreDataPost extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'data.*' => 'required'
        ];
    }
}

data.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Send Data</title>
</head>
<body>
    <form action="{{ route("store") }}" method="POST">
        @csrf
        
        {{-- Data in field 1 in DB --}
        <div>
            <input type="number" name="data[]" id="data">
        </div>

        {{-- Data in field 2 in DB --}
        <div>
            <input type="number" name="data[]" id="data">
        </div>

        <input type="submit" class="btn btn-primary" value="Enviar"> 
    </form>
</body>
</html>

Advertisement

Answer

The problem is that it’s trying to insert array inside an integer column, Try this instead the code in your store method:

public function store(StoreDataPost $request)
{
    foreach (request('data') as $val) {
        Data::create(['data' => $val]);
    }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement