Doesn't your code working?
how to delete data and update data from two different tables at the same time ?
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', 'UserController@destroy');
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>
Please or to participate in this conversation.