I tried to create an editable role in the user table but I got an error and the data was not updated to the database
This is an error
This is my blade
<div class="table-responsive"> @csrf <table id="editable" class="table table-bordered table-striped"> <thead> <tr> <th>ID</th> <th>First Name</th> <th>Email</th> <th>Role</th> </tr> </thead> <tbody> @foreach($allusers as $row) <tr> <td>{{ $row->id }}</td> <td>{{ $row->name }}</td> <td>{{ $row->email }}</td> <td>{{ $row->role }}</td> </tr> @endforeach </tbody> </table> </div>
This is my controller route
Route::post('tabledit/action', 'App\Http\Controllers\EventController@action')->name('tabledit.action');
This is my function in EventController
function action(Request $request) { if($request->ajax()) { if($request->action == 'edit') { $data = $request->role; $updaterole = DB::table('users') ->where('id', $request->id) ->first(); $updaterole = $data; $update->update(); } if($request->action == 'delete') { DB::table('users') ->where('id', $request->id) ->delete(); } return response()->json($request); } }
This is my opinion
Can anyone help me
P粉9532317812024-02-26 10:05:41
It is better to use if request has instead of request action.
if($request->has('edit') { // }
function action(Request $request) { if($request->ajax()) { if($request->action == 'edit') { $data = $request->role; DB::table('users')->where('id', $request->id)->update(['role' => $data]); } if($request->action == 'delete') { DB::table('users')->where('id', $request->id)->delete(); } return response()->json($request); } }