Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda