简体   繁体   中英

General error: 1366 Incorrect integer value Laravel

I'm stuck 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.

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

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.

Any help will be appreciated a lot.

With the get() method, you are selecting rows corresponding to you query in the table, but the foreign key should be the id only.

So you need to take only one row (using first() considering you should have only one row) and select only the 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM