2

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

5
  • show controller method and what you have tried so someone can help you to solve issue Commented Jul 2, 2021 at 13:20
  • Hi john I have updated the question Commented Jul 2, 2021 at 13:27
  • what you get if you dd($location ); i mean dd($request->all()); Commented Jul 2, 2021 at 13:27
  • Does this work? Location::create(['address'=>$request->value,'long'=>$request->longitude,'lat'=>$request->latitude]); Commented Jul 2, 2021 at 13:29
  • If thats all your data on the database too you could do Location::create($request->all); Otherwise just try to dd($request->all); Commented Jul 2, 2021 at 13:32

3 Answers 3

4

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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]);
}

2 Comments

but i got "Cannot use object of type stdClass as array" when i try to decode it
@AfzalAli.try sample test from my answer.so you can verifiy.are you sure $request->all return json
0

If you have a json like this

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

you can do like this to access the values

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

the you can access the values like this

$json = json_decode($json, true);
echo $json['value'];
echo $json['latitude'];
echo $json['latitude'];

I hope it helps some one

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.