gpt4 book ai didi

php - 一般错误 : 1366 Incorrect integer value Laravel

转载 作者:行者123 更新时间:2023-11-29 10:41:01 25 4
gpt4 key购买 nike

我被这个错误困扰了。我稍微解释一下这个观点。

我有一个表单来创建一个新项目,当我创建它时,我隐藏该表单并保存它,然后渲染另一个表单来创建翻译,我创建它并保存它,最后渲染另一个表单表单来创建客户端,我创建并保存它。

此时,就在同一个函数中,我想在另一个表中保存另一个值。

我想将数据库中最后保存的client_id和project_id的值保存在client_projects中。

所以我尝试这个:

$client_project = new Client_Project();
$client_project->client_id = DB::table('clients')->where('id', DB::raw("(select max(id) from clients)"))->get();
$client_project->project_id = DB::table('projects')->where('id', DB::raw("(select max(id) from projects)"))->get();
$client_project->save();

但它给了我标题的错误::

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '[{"id":49,"name":"hola","slug":"hola","priority":1,"created_at":"2017-08-03 13:53:35","updated_at":"2017-08-03 13:53:35"}]' for column 'client_id' at row 1 (SQL: insert into `client_project` (`client_id`, `project_id`, `updated_at`, `created_at`) values ([{"id":49,"name":"hola","slug":"hola","priority":1,"created_at":"2017-08-03 13:53:35","updated_at":"2017-08-03 13:53:35"}], [{"id":40,"slug":"1","order":40,"public":1,"pathheader":"\/tmp\/phpf8NUkb","pathhome":"\/tmp\/php9zepk7","created_at":"2017-08-03 13:49:53","updated_at":"2017-08-03 13:49:53"}], 2017-08-03 13:53:35, 2017-08-03 13:53:35))

我创建客户端并尝试创建 client_projects 的完整功能是这样的

public function storeProjectsClients(Request $request) {
$client = new Client();
$client->name = $request->input("nameClient");
$client->slug = $request->input("slugClient");
$client->priority = $request->input("priorityClient");
$client->save();
$client_project = new Client_Project();
$client_project->client_id = DB::table('clients')->where('id', DB::raw("(select max(id) from clients)"))->get();
$client_project->project_id = DB::table('projects')->where('id', DB::raw("(select max(id) from projects)"))->get();
$client_project->save();
}

我知道最好使用 json 传递 project_id 和 client_id 的值,因为如果某些用户同时这样做会出现问题,但无论如何,这仅适合我。

任何帮助将不胜感激。

最佳答案

使用 get() 方法,您可以选择与表中查询相对应的行,但外键只能是 id

因此,您只需要获取一行(考虑到您应该只有一行,使用 first() )并仅选择 id。

您可以尝试以下代码:

$client_project = new Client_Project();
$client_project->client_id = DB::table('clients')->where('id', DB::raw("(select max(id) from clients)"))->first()->id;
$client_project->project_id = DB::table('projects')->where('id', DB::raw("(select max(id) from projects)"))->first()->id;
$client_project->save();

关于php - 一般错误 : 1366 Incorrect integer value Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45486454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com