On my website, users can reply to statuses.
$reply = Status::create([
'body' => $replyText,
])->user()->associate(Auth::user());
status->replies()->save($reply);
The request is sent through AJAX. If the request succeeds, the reply is generated automatically on the DOM without a page redirect.
And that's cool and all, but I also want users to be able to delete/edit replies they just made! And for that, I would need the ID in my status table of the newly created reply.
Something like...
$reply = Status::create([
'body' => $replyText,
])->user()->associate(Auth::user());
$status->replies()->save($reply);
$replyID = ID of the status I just saved to database;
return response()->json($replyID);
Update:
New code based on Eddy's response:
$reply = Status::create([
'body' => $replyText,
])->user()->associate(Auth::user());
$status->replies()->save($reply);
$id = $reply->id;
return response()->json($id);
Does not return anything. If I do $id = "test", however, the JSON response will be "test". So it doesn't look like that's working.
$reply = Status::create(...); //returns the created object with ID
$id = $reply->id;
You can either return the $id or simply return the object itself.
return response()->json($reply); //or $id
Just console.log() the response to see what you return and use it.