Home  >  Q&A  >  body text

mistake. How can I update and delete live table using ajax in laravel

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粉805931281P粉805931281260 days ago382

reply all(1)I'll reply

  • P粉953231781

    P粉9532317812024-02-26 10:05:41

    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);
            }
        }

    reply
    0
  • Cancelreply