我打算使用这个package,因为它看起来很适合我正在寻找的东西。 (它为Follows / Likes / Votes添加了一个基于用户的跟随系统)
我已根据他们的文档将特征添加到我的用户模型和专业人员模型中,并在专业人员页面上创建了一个按钮:
<a href="{{ url('professionals/'.$professional->category->slug.'/'.$professional->slug.'/unfollow') }}" class="btn btn-sm white">UnFollow</a>
<a href="{{ url('professionals/'.$professional->category->slug.'/'.$professional->slug.'/unfollow') }}" class="btn btn-sm white">UnFollow</a>
我在路线文件中定义了以下内容:
Route::post('professionals/{professionalcategory}/{slug}/follow', 'ProfessionalController@followProfessional')->name('professional.follow');
Route::post('proffessionals/{professionalcategory}/{slug}/unfollow', 'ProffessionalController@unFollowProfessional')->name('professional.unfollow');
我的控制器中有以下方法:
public function followProfessional($slug)
{
$professional = Professional::where('slug', $slug)->first();
$user = Auth::user();
$user->follow($professional);
return redirect()->back()->with('success', 'You are currently following this professional.');
}
public function unFollowProfessional($slug)
{
$professional = Professional::where('slug', $slug)->first();
if(! $professional) {
return redirect()->back()->with('error', 'Professional does not exist.');
}
$user = Auth::user()->id;
$user->follow($professional);
return redirect()->back()->with('success', 'Successfully unfollowed the professional.');
}
当我点击关注按钮时,我收到此错误:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
没有消息
我的第二个问题是:
我还希望实现用户点击&#34;关注&#34;并且被添加到关注者列表中,他应该看到一个按钮&#34;取消关注&#34;而不是跟随,反之亦然。
我试过了:
@if($professional->isFollowedBy($user))
<a href="{{ url('professionals/'.$professional->category->slug.'/'.$professional->slug.'/unfollow') }}" class="btn btn-sm white">Un Follow</a>
@else
<a href="{{ url('professionals/'.$professional->category->slug.'/'.$professional->slug.'/follow') }}" class="btn btn-sm white">Follow</a>
@endif
但这也不起作用。这里我的错误是什么?
答案 0 :(得分:0)
由于您的路线是Route::post()
但是你把这些称为get。
将您的路线更改为此。
Route::get('professionals/{professionalcategory}/{slug}/follow', 'ProfessionalController@followProfessional')->name('professional.follow');
Route::get('proffessionals/{professionalcategory}/{slug}/unfollow', 'ProffessionalController@unFollowProfessional')->name('professional.unfollow');
希望这有帮助
答案 1 :(得分:0)
尝试了下面的代码,终于工作了!
public function followProfessional(ProfessionalCategory $professionalcategory, $slug)
{
$professional = Professional::where('slug', $slug)->first();
$user = Auth::user();
$user->follow($professional);
return redirect()->back()->with('success', 'You are now following this professional.');
}
public function unFollowProfessional(ProfessionalCategory $professionalcategory, $slug)
{
$professional = Professional::where('slug', $slug)->first();
$user = Auth::user();
$user->unfollow($professional);
return redirect()->back()->with('success', 'You have unfollowed this professional.');
}
并在视图中
@if (Auth::user()->isFollowing($professional))
<a href="{{ url('professionals/'.$professional->category->slug.'/'.$professional->slug.'/unfollow') }}" class="btn btn-sm white">UnFollow</a>
@else
<a href="{{ url('professionals/'.$professional->category->slug.'/'.$professional->slug.'/follow') }}" class="btn btn-sm white">Follow</a>
@endif
答案 2 :(得分:0)
第二个问题。 如果要在单击按钮后看到更改后续/取消关注按钮,则需要刷新页面。
否则,如果您想立即看到按钮的更改,则需要发送ajax请求。并将按钮文本更改代码放入回调函数中。