2

I use this method:

public function store(CreateEvent $request) { dd($request->json()->all()); } 

My requests is:

{"name":"etegjgjghjghj","date":"2019-03-08"} 

Headers:

Accept: application/json, text/plain, */* Content-Type: application/json Origin: http://localhost:4200 

As response I get blank page in Chrome network without response data.

I tried this:

public function store(CreateEvent $request){ dd('test'); } 
3
  • You want to convert your inputs to a json? Commented Mar 1, 2019 at 20:25
  • Then your data isn't passed correctly Commented Mar 1, 2019 at 20:32
  • What is CreateEvent Class? Never heard before Commented Mar 1, 2019 at 20:35

2 Answers 2

13

try this:

public function store(CreateEvent $request) { return response()->json($request->all()); } 
Sign up to request clarification or add additional context in comments.

4 Comments

It works, so to get specific property should I do: $request->date?
If it's json it's better to use $request->input('date'); but you can use also $request->date. You can see here laravel.com/docs/5.7/requests#retrieving-input
Why I can not get ` dd($request->name);`, it return blank
With json data and Content/Type set to application/json the advised way to retrieve input is with $request->input('date') you can read in the doc I linked above
2

If the request has header 'Content-Type: application/json' and it's a valid JSON, then laravel will convert it automatically. You don’t need to do any extra job.
But you have to make sure the JSON is correct. Because JSON must contain double quoted strings not single (if has any)
Next thing, your form validation probably shooting 422 request which by default redirects back to previous page. you can try dd in the form request class

1 Comment

I was using single quotes. Chaging for double quotes resolved the issue for me. Thx.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.