如何使用 laravel 删除数据透视表 table 中的单行
How do delete single row in pivot table with laravel
我想删除数据透视表中的单行数据 table。我没有收到任何错误,但是当我尝试点击按钮时。它没有将我重定向到任何地方,因此没有执行删除功能。
在上图中我想删除 user_id = 3
的突出显示的 id
我的情况是,用户突然无法参加 even_id = 6
,所以他想参加 delete/unjoined 活动。
route
Route::get('/user/event/{event}', 'HomeController@destroy')->name('user.event.destroy');
blade
@foreach ($events as $event)
<tr>
<td>{{$loop->index +1 }}</td>
<td>{{$event->event_name}}</td>
<td>{{$event->event_date}}</td>
<td>
<form method="POST" action="{{ route('user.event.destroy',$event)}}">
@csrf
@method('DELETE')
<a class="btn btn-danger">Unjoined!</a>
</form>
</td>
</tr>
@endforeach
controller
public function storeEventUser(Request $request)
{
$user = User::find(Auth::user()->id);
//how I storing my pivot data (just to show if anyone asking)
$user->events()->syncWithoutDetaching([$request->event_id]);
}
public function destroy($event)
{
$event= Event::findOrFail($event_id);
$user->events()->detach($event);
return redirect()->back()->with('success','Deleted.');
}
Event model
public function users()
{
return $this->belongsToMany(User::class,'event_user','event_id','user_id');
}
user model
public function events()
{
return $this->belongsToMany(Event::class,'event_user','user_id','event_id');
}
为了简单起见,我正在调整您的控制器方法以使用路由模型绑定:
public function destroy(Event $event)
{
Auth::user()->events()->detach($event);
// or from the other side of the relationship
// $event->users()->detach(Auth::user());
return redirect()->back()->with('success', 'Deleted.');
}
如评论中所述,如果您想使用表单通过 @method('DELETE')
blade指令。
旁注:
Auth::user()
returns 一个 User
实例,因此您无需在 storeEventUser
方法中再次查询它:
$user = Auth::user();
我想删除数据透视表中的单行数据 table。我没有收到任何错误,但是当我尝试点击按钮时。它没有将我重定向到任何地方,因此没有执行删除功能。
在上图中我想删除 user_id = 3
的突出显示的 id
我的情况是,用户突然无法参加 even_id = 6
,所以他想参加 delete/unjoined 活动。
route
Route::get('/user/event/{event}', 'HomeController@destroy')->name('user.event.destroy');
blade
@foreach ($events as $event)
<tr>
<td>{{$loop->index +1 }}</td>
<td>{{$event->event_name}}</td>
<td>{{$event->event_date}}</td>
<td>
<form method="POST" action="{{ route('user.event.destroy',$event)}}">
@csrf
@method('DELETE')
<a class="btn btn-danger">Unjoined!</a>
</form>
</td>
</tr>
@endforeach
controller
public function storeEventUser(Request $request)
{
$user = User::find(Auth::user()->id);
//how I storing my pivot data (just to show if anyone asking)
$user->events()->syncWithoutDetaching([$request->event_id]);
}
public function destroy($event)
{
$event= Event::findOrFail($event_id);
$user->events()->detach($event);
return redirect()->back()->with('success','Deleted.');
}
Event model
public function users()
{
return $this->belongsToMany(User::class,'event_user','event_id','user_id');
}
user model
public function events()
{
return $this->belongsToMany(Event::class,'event_user','user_id','event_id');
}
为了简单起见,我正在调整您的控制器方法以使用路由模型绑定:
public function destroy(Event $event)
{
Auth::user()->events()->detach($event);
// or from the other side of the relationship
// $event->users()->detach(Auth::user());
return redirect()->back()->with('success', 'Deleted.');
}
如评论中所述,如果您想使用表单通过 @method('DELETE')
blade指令。
旁注:
Auth::user()
returns 一个 User
实例,因此您无需在 storeEventUser
方法中再次查询它:
$user = Auth::user();