в вашем маршрутах / web.php
Route::post('/edit','AjaxController@getProfessions')->name('edit');
в вашем контроллере:
use Illuminate\Support\Facades\Validator;
public function getProfessions(Request $request)
{
try {
$validator = Validator::make($request->all(), [
'postId' => 'required',
'body' => 'required'
]
);
if ($validator->fails()) {
$response=array('status'=>'error','errors'=>implode(',', $validator->errors()->all()));
return response()->json($response, 200);
}else{
$profession = Profession::where(['categories_id'=>$request->input('postId')])->first();
if($profession){
$profession->body=$request->input('body');
$profession->save();
return response()->json(['profession'=>$profession], 200);
}else{
$response=array('status'=>'error','errors'=>'profession not found');
return response()->json($response, 200);
}
}
}catch(\Exception $e){
$response=array('status'=>'error','errors'=>'Internal Server Error');
return response()->json($response, 500);
}
}
в представлении редактирования блейда, вы передаете $ профессиюзатем просмотрите:
<meta name="csrf-token" content="{{ csrf_token() }}" />
<form>
<input type="hidded" id="postID" name="postID" value="{{$profession->postID}}" />
<input type="text" id="body" name="body" value="{{$profession->body}}" />
<button type="button" id="testAjax">Submit using AJAX</button>
</form>
в вашей функции ajax:
$('#testAjax').on('click',function(){
var postID=$('#postID').val();
var body=$('#body').val();
$.ajax({
url:"{{ route('edit')}}",
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type:"POST",
dataType:"json",
data:{
postId:postID,
body:body,
},
success:function(response){
console.log(response);
},
error:function(err){
}
});
});