i have following code in controller
public function responcename(request $request) {
$data = request()->all();
//$name = $data['name'];
$user = Auth::user();
$userId = $user->email;
/////DB::update('update users set tutorial_detail = ? where email = ?',[$data['name'], $userId]);
//$tutorial_name = DB::select('select tutorial_detail from users where id = ?', array($userId));
return response()->json(['tutorialname' => $data['name']]);
}
and route is
Route::get('change_tutorialname', 'teacher@edittutorialname');
Route::post('/change_tutorialname','teacher@responcename');
when i comment on $user = Auth::user(); $userId = $user->email; it works properly.. but when i uncomment it show error message from ajax below is ajax code
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(".btn-submit").click(function(e){
e.preventDefault();
var name = $("input[name=name]").val();
$.ajax({
type:'POST',
url:'/change_tutorialname',
data: {name: name},
success:function(data){
$("form").css("display", "none");
alert(data.tutorialname);
},
error: function(){
alert("Record not deleted.")
},
});
});
Instead of
DB::update('update users set tutorial_detail = ? where email = ?',[$data['name'], $userId]);
Try:
DB::statement('update users set tutorial_detail = ? where email = ?',[$data['name'], $userId]);
You cold try this:
public function responcename(request $request) {
User::find(Auth::id())->update([
'name' => $request->name
]);
return response()->json(['tutorialname' => $request->name]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.