gpt4 book ai didi

php - 如何在laravel中同时从两个不同的表中删除数据和更新数据?

转载 作者:行者123 更新时间:2023-11-29 15:15:13 25 4
gpt4 key购买 nike

我是 Laravel 的学生和新手`我有一个音乐应用程序,具有三种不同的用户类型,即 type_id = 3 的“user”、type_id = 2 的“DJ”和 type_id = 1 的“admin”

pada halaman 管理员 terdapat daftar 用户 dan juga DJ dengan aksi“hapus”

DJ 列表

ID_______| 1 __________ | 2 __________ |    
NAME____ | john_______ | jane________ |
CHANNEL | john channel | jane channel |
ACTION__ | delete ______| delete ______|

用户列表

ID _____| 3 ____ | 4 ____ |
NAME__ | dave__ | mike__ |
ACTION | delete | delete |

这就是我需要的

如果管理员点击用户的“删除”,我只想删除用户表上的所有数据

如果管理员点击DJ上的“删除”,我想从users表中删除他的所有数据,并将 channel 表中与DJ的id值相同的user_id更新为“NULL” ”

我该怎么做才能得到这些东西?

用户表

ID______| 1___ | 2___ | 3___ | 4____|
TYPE_ID | 2___ | 2___ | 3___ | 3____|
NAME __ | john | jane | dave | mike |

channel 表

ID______| 1__________  | 2__________  | 3_________ |
USER_ID | 1__________ | 2__________ | NULL_____ |
NAME __ | john channel | jane channel | Channel #3 |

这是我的 Controller

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

路线/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>

最佳答案

//delete
public function destroy(Post $post)
{
// Delete for image coding
if(Storage::disk('public')->exists('uploads/post/'.$post->image))
{
Storage::disk('public')->delete('uploads/post/'.$post->image);
}
// Delete code for zipfile
if(Storage::disk('public')->exists('uploads/zip/'.$post->zip))
{
Storage::disk('public')->delete('uploads/zip/'.$post->zip);
}
$post->categories()->detach();
$post->tags()->detach();
$post->delete();
Toastr::success('Post Successfully Deleted :)','Success');
return redirect()->back();
}

//update

public function update(Request $request, Post $post)
{
$this->validate($request,[
'title' =>'required',
'image' => 'image',
'categories' => 'required',
'tags' => 'required',
'body' => 'required',
'live_demo' =>'required'
]);





$image = $request->file('image');
$slug = str_slug($request->title);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1600,1066);
if (!file_exists('storage/uploads/post'))
{
mkdir('storage/uploads/post',0777,true);
}
unlink('storage/uploads/post/'.$post->image);
$image_resize->save('storage/uploads/post/'.$imagename);
}else{
$imagename = $post->image;
}

$post->user_id = Auth::id();
$post->title = $request->title;
$post->slug = str_slug($request->title);
$post->image = $imagename;
$post->zip = $zipname;
$post->body = $request->body;
$post->price = $request->price;
$post->live_demo = $request->live_demo;
if(isset($request->status))
{
$post->status =true;
}else
{
$post->status = false;
}
$post->is_approved = true;
$post->save();
$post->categories()->sync($request->categories);
$post->tags()->sync($request->tags);



Toastr::success('Post Successfully Updated:)','Success');
return redirect()->route('admin.post.index');
}

//blade
<button class="btn btn-danger waves-effect" type="button" onclick="deletepost({{ $post->id }})">
<i class="material-icons">{{__('delete')}}</i>
</button>
<form id="delete-form-{{ $post->id }}" action="{{ route('admin.post.destroy',$post->id) }}" method="POST" style="display: none;">
@csrf
@method('DELETE')
</form>

关于php - 如何在laravel中同时从两个不同的表中删除数据和更新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59733135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com