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

269
Visualizações
Laravel: Access key value from json type string

I got this data from request in Laravel

{"value":"QWS Welding Supply Solutions, Taylor Street","latitude":-27.4495148,"longitude":153.0696076}

but i am not able to access keys individually i.e value, latitude, longitude. I have tried json_decode but that's not working.

This is my controller method

public function saveLocation(Request $request){
    $location = $request->all();
    Location::create(['address'=>$location->value,'long'=>$location->longitude,'lat'=>$location->latitude]);
}

basically i want to save location data from request to database. in the $location object i got data that i pasted on top

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I don't know what's wrong with simple json_decode but I resolved my issue by using json_decode inside foreach loop and then accessing key pair values

foreach (json_decode($location) as $key => $value) {
    if($key=="value")
        $obj['address'] = $value;
    else
        $obj[$key] = $value;
}
Location::create($obj);
over 4 years ago · Santiago Trujillo Relatório

0

Json decode works properly for mentioned json.If $location return mentioned json then

$data='{"value":"QWS Welding Supply Solutions, Taylor Street","latitude":-27.4495148,"longitude":153.0696076}';

dd(json_decode($data)->latitude);

so it should be

public function saveLocation(Request $request){
    $location = json_decode($request->all());
    Location::create(['address'=>$location->value,'long'=>$location->longitude,'lat'=>$location->latitude]);
}
over 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