mistake. How can I update and delete live table using ajax in laravel
P粉805931281
P粉805931281 2024-02-25 17:25:30
0
1
403

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粉805931281
P粉805931281

reply all(1)
P粉953231781

It is better to use if request has instead of request action.

if($request->has('edit')
{
//
}

But I didn't change it. Only the updated section has been edited.

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);
        }
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template