将 nvarchar 转换为 int 时出错...作为数字? - Laravel、SQL
Error converting nvarchar into int... being a number? - Laravel, SQL
我正在创建一个 json 来保存具有相同名称 (data
) 的表单的 2 个值,以便在 table 中保存 2 行而不是同时只有一个。
这个结构是这样创建的,不能编辑。
原则上是可行的,但最后它给了我一个错误,我不太确定这是一个 Laravel 问题还是只是 SQL,所以我不知道在哪里去。
我试过两次保存值“1”,看看结果如何,结果出现了以下错误:
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"]))
数据库和项目本身再简单不过了,所以我不知道该去哪里,知道吗?
数据库结构
- ID (INT, Primary Key, AUTOINCREMENT)
- Data (INT)
DataController.php
namespace App\Http\Controllers\data;
use App\Data;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\StoreDataPost;
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 Illuminate\Database\Eloquent\Model;
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 App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
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>
问题是它试图在整数列中插入数组,试试这个而不是存储方法中的代码:
public function store(StoreDataPost $request)
{
foreach (request('data') as $val) {
Data::create(['data' => $val]);
}
}
我正在创建一个 json 来保存具有相同名称 (data
) 的表单的 2 个值,以便在 table 中保存 2 行而不是同时只有一个。
这个结构是这样创建的,不能编辑。
原则上是可行的,但最后它给了我一个错误,我不太确定这是一个 Laravel 问题还是只是 SQL,所以我不知道在哪里去。
我试过两次保存值“1”,看看结果如何,结果出现了以下错误:
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"]))
数据库和项目本身再简单不过了,所以我不知道该去哪里,知道吗?
数据库结构
- ID (INT, Primary Key, AUTOINCREMENT)
- Data (INT)
DataController.php
namespace App\Http\Controllers\data;
use App\Data;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\StoreDataPost;
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 Illuminate\Database\Eloquent\Model;
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 App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
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>
问题是它试图在整数列中插入数组,试试这个而不是存储方法中的代码:
public function store(StoreDataPost $request)
{
foreach (request('data') as $val) {
Data::create(['data' => $val]);
}
}