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
via Cr1xus