how to delete data and update data from two different tables at the same time ?

Published 1 week ago by wardaddy

I have a music app with three different user types, namely "user" with type_id = 3, "DJ" with type_id = 2, and "admin" with type_id = 1

pada halaman admin terdapat daftar user dan juga DJ dengan aksi "hapus"

DJ List

  • ID_______| 1 __________| 2 __________|

  • NAME____| john_______| jane________|

  • CHANNEL | john channel | jane channel |

  • ACTION__| delete ______| delete ______|

User List

  • ID _____| 3 ____| 4 ____|

  • NAME__| dave__| mike__|

  • ACTION | delete | delete |

this is what i need

if the admin clicks "delete" on the user, I just want to delete all the data itself on the users table

and if the admin clicks on "delete" on the DJ, I want to delete all his data from the users table and also update the user_id whose value is the same as the id of the DJ on the channel table to "NULL"

what can i do to get those things ?

Users Table

  • ID______| 1___| 2___| 3___| 4____|

  • TYPE_ID | 2___| 2___| 3___| 3____|

  • NAME __| john | jane | dave | mike |

Channels Table

  • ID______| 1__________| 2__________| 3_________|

  • USER_ID | 1__________| 2__________| NULL_____|

  • NAME __| john channel | jane channel | Channel #3 |

here my code

UserController.php

public function index()
{
     $djs = User::join('channels', 'users.id', '=', 'channels.user_id')
                     ->select('users.*', 'channels.name as channel_name')
                     ->where('type_id', '=', '2')
                     ->orderBy('updated_at', 'desc')
                     ->get();
     $users = User::where('type_id', '=', '3')->orderBy('created_at', 'desc')->get();

     return view('dashboard.manageUsers', compact('users', 'djs'));
}

public function destroy(channel $channel, $id)
{
    User::find($id)->delete();

    $channel = App\Channel::find($channel->id);
    $channel->user_id = NULL;
    $channel->save();

    return redirect('/dash/manage-users');
}

routes/web.php

Route::delete('/users/{id}/delete', '[email protected]');

manageUsers.blade.php

<h5 class="light">DJ List</h5>
<table>
    <thead>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>channel</th>
            <th>action</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($djs as $dj)
                <tr>
                    <td>{{ $dj->id }}</td>
                    <td>{{ $dj->name }}</td>
                    <td>{{ $dj->channel_name }}</td>
                    <td>
                        <form method="POST" action="/users/{{ $dj->id }}/delete">
                        {{ csrf_field() }}
                            {{ method_field('DELETE') }}
                        <button class="btn-flat">delete</button>
                    </form>
                    </td>
            </tr>
        @endforeach
    </tbody>
</table>

<h5 class="light">User List</h5>
<table>
    <thead>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>action</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($users as $user)
                <tr>
                    <td>{{ $user->id }}</td>
                    <td>{{ $user->name }}</td>
                    <td>
                        <form method="POST" action="/users/{{ $user->id }}/delete">
                        {{ csrf_field() }}
                            {{ method_field('DELETE') }}
                        <button class="btn-flat">delete</button>
                    </form>
                    </td>
            </tr>
        @endforeach
    </tbody>
</table>
tisuchi
tisuchi
1 week ago (235,315 XP)

Doesn't your code working?

wardaddy

@tisuchi there was an error on my code

when I try to delete the user it says

FatalErrorException

Call to a member function delete () on null

and when I tried removing the DJ it said

(1/2) PDOException

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (discjoke.channels, CONSTRAINT channels_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (id))

(2/2) QueryException

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (discjoke.channels, CONSTRAINT channels_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (id)) (SQL: delete from users where id = 4)

tisuchi
tisuchi
1 week ago (235,315 XP)

Make sure that, you are passing two parameters for destroy() method.

Try this-

public function destroy($channelid, $id)
{
    $hasUser = User::find($id);
    if($hasUser){
        $hasUser->delete();
    }

    $channel = Channel::find($channelid);
    if($channel) {

        //make sure user_id can be nullable in channels table. 
        $channel->user_id = NULL;
        $channel->save();   
    }

}


wardaddy

@tisuchi what is the purpose of $ channelid

I have tried your code, I tried to delete user and DJ but there is still error there

public function destroy($channelid, $id)
{
    $hasUser = User::find($id);
    if($hasUser){
        $hasUser->delete();
    }

        $channel = Channel::find($channelid);
        if($channel) {
        $channel->user_id = NULL;
        $channel->save();
    }

    return redirect('/dash/manage-users');
}

it says

(1/1) ErrorException

Missing argument 2 for App\Http\Controllers\UserController::destroy()

Please sign in or create an account to participate in this conversation.