我遇到了这个错误。
我稍微解释一下这个观点。
我有一个表单来创建一个新项目,当我创建它时,我隐藏表单并保存它,然后我渲染另一个表单来创建翻译,我创建它并保存它,最后我渲染另一个用于创建客户端的表单,我创建并保存它。
此时,只是在同一个函数中,我想在另一个表中保存另一个值。
我想在client_projects中保存保存在数据库中的最后一个client_id和project_id的值。
所以我试试这个:
$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的值会更好,因为如果某些用户在同时执行此操作会产生问题,但无论如何,这只适用于我。
任何帮助都会受到很多赞赏。
答案 0 :(得分:1)
使用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();