I'm stocked with this error.
我有这个错误。
I explain the view a little.
我稍微解释一下这个观点。
I have a form to create a New project, when i created it i hide the form and i save it, then i render an other form to create translation, i create it and i save it, and finally i render an other form to create client, i create and it save it.
我有一个表单来创建一个新项目,当我创建它时,我隐藏表单并保存它,然后我渲染另一个表单来创建翻译,我创建它并保存它,最后我渲染另一个表单来创建客户端,我创建并保存它。
At this moment, just in the same function, i want to save another value in another table.
此时,只是在同一个函数中,我想在另一个表中保存另一个值。
I want to save in client_projects the value of the last client_id and project_id saved in database.
我想在client_projects中保存保存在数据库中的最后一个client_id和project_id的值。
So i try this:
所以我试试这个:
$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();
But it give me the error of the title::
但它给了我标题的错误::
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))
The full function where i create the client and i try to create the client_projects is this
我创建客户端并尝试创建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();
}
I know it's better to pass using json the value of the project_id and client_id, cause if some users are doing this at the same time it will give problems, but anyway, this is only for me.
我知道最好使用json传递project_id和client_id的值,因为如果有些用户同时执行此操作会产生问题,但无论如何,这只适用于我。
Any help will be appreciated a lot.
任何帮助将不胜感激。
1 个解决方案
#1
1
With the get()
method, you are selecting rows corresponding to you query in the table, but the foreign key should be the id
only.
使用get()方法,您将在表中选择与您查询相对应的行,但外键应仅为id。
So you need to take only one row (using first()
considering you should have only one row) and select only the id.
所以你只需要一行(使用first()考虑你应该只有一行)并只选择id。
You can try the following code :
您可以尝试以下代码:
$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();
#1
1
With the get()
method, you are selecting rows corresponding to you query in the table, but the foreign key should be the id
only.
使用get()方法,您将在表中选择与您查询相对应的行,但外键应仅为id。
So you need to take only one row (using first()
considering you should have only one row) and select only the id.
所以你只需要一行(使用first()考虑你应该只有一行)并只选择id。
You can try the following code :
您可以尝试以下代码:
$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();