2

I am using laravel 5.3 and angularjs I submit json from my angularjs like below

{"grc":{"id":1},"floatingGrcs":[{"days":"10","units":"100"},{"days":"20","units":"200"}]}

I accept this array from my laravel controller like below

public function store(Request $request)
{
   //how to extract $request object in here
}

I don't know how to extract submitted json array in laravel controller

0

3 Answers 3

2

You can use standard json_decode():

public function store(Request $request)
{
    $data = json_decode($request->someJson);
}

You can look at all available data with dd($request->all());

Sign up to request clarification or add additional context in comments.

Comments

2

The common-case is that there is no need to post json-encoded data through Angular.

To submit something with Angular, you should use, for example:

$http.post('api/something', {"grc":{"id":1},"floatingGrcs": [..]})

And then there is no need to decode json on the Laravel side:

public function store(Request $request)
{
   $request->all();     // to get all fields
   $request->grc->id;   // to get a specific field
}

Comments

0

I got answer with below code. But i am not sure

$params = json_decode(file_get_contents('php://input'), TRUE);

is this above line of code secure or not.

public function store()
    {

        $params = json_decode(file_get_contents('php://input'), TRUE);

        foreach ($params as $key => $value) 
        {
            if($key == "grc")
            {
                $grc_id = $value["id"];
            }
            elseif($key == "floatingGrcs")
            {
                foreach ($value as $floating) 
                {
                    $days = $floating["days"];

                }
            }

        }

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.