【发布时间】:2021-09-09 11:32:04
【问题描述】:
我正在我的表单中提出发布请求。 csrf token 和方法都在那里。
<div class="flex">
<form action="{{ route('profile.store.follower', $user) }}" method="post">
@csrf
<input value="follow" type="hidden" name="follow" value="{{$user->id}}" >
<button type="submit" class="bg-green-700">Follow</button>
</form>
</div>
路线:
Route::get('/usersprofile/{user:name}/index', [ProfileController::class, 'index'])->name('profile.index');
Route::post('/usersprofile/{user:name}/index', [ProfileController::class, 'store'])->name('profile.store.follower');
控制器:
public function store(User $user, Request $request) {
dd($user);
$user = user::findOrFail($request->follow);
Auth::user()->following->attach($user->id);
return back();
}
用户模型中的以下函数:
public function following() {
return $this->belongsToMany(User::class, 'followers', 'user_id', 'following_id');
}
由于某种原因,它没有通过。我试过php artisan route:cache 和config:cache 但错误仍然存在。我检查了路线列表,路线存在。
【问题讨论】:
-
我认为你不需要
user:name只需user就足够了 -
我仍然遇到同样的错误
-
你能 dd($user) 告诉我们它的价值吗
-
它没有通过。 dd($user) 是控制器中当前的内容,但我仍然收到 404 错误
-
就我个人而言,我不会使用
store()方法,而是用于关注用户的专用方法(follow()或其他东西)。删除隐藏的输入和$user = user::findOrFail($request->follow);行,因为您要关注的用户已经存在于$user中。然后使用Auth::user()->following->attach($user);
标签: laravel