4

Route:

Route::post('admin/cms/create','CmsController@createmenu'); 

Controller's action:

public function createmenu(Request $request){ $menu = new menu; $this->validate($request,[ 'name'=>'required', 's_title'=>'required_if:s_exist,1', 's_desc'=>'required_if:s_exist,1', 's_path'=>'required_if:s_exist,1', 'category'=>'required' ]); $path=$request->file('s_path')->store('img/slideshow'); $menu::create([ 'name'=>$request->name, 's_exist'=>$request->s_exist, 's_title'=>$request->s_title, 's_desc'=>$request->s_desc, 's_path'=>$path, 'category'=>$request->category ]); return redirect('admin/cms'); } 

Ajax (jquery):

$("#f_ins_menu").on("submit",function(e){ e.preventDefault(); var data={}; $.ajax({ type:"POST", //url:$(this).attr("action"), //I would like it to work with this dynamic url url:"cms/create", //this file is inside the folder admin //I also tried without the map function data:$(this).serializeArray().map(function(x){data[x.name] = x.value;}), contentType:false, cache:false, processData:false, }); }); 

The createmenu action works without the ajax

I have another action that uses Ajax and it works, but the difference is: in that action I don't pass Request $request, in fact I don't pass anything.

I have tried in native PHP and it works. Note that I added contentType:false, cache:false, processData:false, because I also pass a file.

I have tried to die and dumb (dd) the $request param and I get a huge block of code, I think a class, so my thoughts are that Request $request param doesn't get the data I pass through ajax

And yes I have included CSRF_FIELD with the meta tag trick that I found here https://laravel.com/docs/5.4/csrf

Can someone help me with at least seeing the data that I pass is array format or json format, doesn't matter.

I don't want you guys to go over all that code. Can you give me the correct version of this function/action.

public function createmenu(Request $request){ return dd($request); //I've tried this but it returns alot of code in console.log when I do the success:function() } 

I just want to see the data that I pass, at least, I can go from there.

EDIT:

Forgot to mention that with the validate method the HTTP response is 422 and without the validation the HTTP response is 500

EDIT 2:

Ok, so do not use die and dumb (dd()) when responding to ajax, just use return and some data. To access the request from ajax I was able to, thanks to @Lorav's comment like this:

data:{data:$(this).serialize()}, 

and

$data = $request->data; 

So it was not a Request class problem, my syntax was wrong.

2
  • 2
    Try $request->all() , this will return an array of parameters that passed in the Request. Commented Apr 3, 2017 at 9:09
  • Ok after a few tweaks I managed to echo out the data I pass with ajax and I can see the $request->all() as it should be, although I'm wondering if it will work with file uploads, I'm not there yet, but post your answer Commented Apr 3, 2017 at 12:06

1 Answer 1

1
+50

Use $request->all() to get posed data as array .

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.