Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda