Multi attach Laravel from ReactJS

Viewed 40

I have a litte problem inputting data from client to server, where the value does not match and has an error. I want to add data using a pivot table with attach but the value I get does not match I tried several ways but failed. What I want after creating the document data then attach the user according to what has been inputted in the client.

The problem

Just want to take id from client for pivot maybe like that:

$document->sent_to()->attach($request->users);

here's the code I've made:

Server

    try {
        $document = Document::create([
            'udin' => $request['udin'],
            'slug' => $uuid,
            'status' => self::$active,
        ]);
       
        $document->sent_to()->attach($request['users']);

        return ResponseFormatter::success(
             DocumentResource::collection([$document])[0],
             self::$saved
        );
    } catch (Exception $e) {
        return ResponseFormatter::error([
            'message' => self::$wrong,
            'error' => $e->errors(),
        ], self::$failed, 500);
    }

Client

enter image description here

1 Answers

SOLVED

First, I manipulate data client-side

Then on the server I use implode() with separator ,

explode(",", $request->users);

After that I do looping for get the result what i want

UPDATED

before sending the data I enter the data into the JSON.stringify() function then in the serve section I use json_decode()

Related