i try to make editable role in user table but its error and not update data to database
here the error
this is my blade
<div >
@csrf
<table id="editable" >
<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>
and here my controller route
Route::post('tabledit/action', 'App\Http\Controllers\EventController@action')->name('tabledit.action');
and here my function inside 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);
}
}
can someone pls help me
CodePudding user response:
It's better to use if request has instead of request action.
if($request->has('edit')
{
//
}
But i didn't change it. Only edited update section.
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);
}
}