Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Send recently saved data from controller to DOM in Laravel

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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

$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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!