上記記事を参考に、フォロー機能の勉強をしています。
フォロー/フォロー解除の部分で、どうしてもエラーになってしまいます。
「フォローする」ボタンを押しても、
404エラーになってしまい、
postで送っているにもかかわらず、
URLが「サーバ名/users//follow?id=8」とget方式になってしまいます。
記事のコードをコピペし直したのですが、
それでも直リませんでした。
原因や対策案をお教えいただけると幸いです。
よろしくお願いいたします。
web.php
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); Route::group(['middleware' => 'auth'], function() { // ユーザ関連 Route::resource('users', 'UsersController', ['only' => ['index', 'show', 'edit', 'update']]); // フォロー/フォロー解除を追加 Route::post('users/{user}/follow', 'UsersController@follow')->name('follow'); Route::delete('users/{user}/unfollow', 'UsersController@unfollow')->name('unfollow'); });
UsersController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; use App\Models\User; use App\Models\Tweet; use App\Models\Follower; class UsersController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(User $user) { $all_users = $user->getAllUsers(auth()->user()->id); return view('users.index', [ 'all_users' => $all_users ]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show(User $user, Tweet $tweet, Follower $follower) { $login_user = auth()->user(); $is_following = $login_user->isFollowing($user->id); $is_followed = $login_user->isFollowed($user->id); $timelines = $tweet->getUserTimeLine($user->id); $tweet_count = $tweet->getTweetCount($user->id); $follow_count = $follower->getFollowCount($user->id); $follower_count = $follower->getFollowerCount($user->id); return view('users.show', [ 'user' => $user, 'is_following' => $is_following, 'is_followed' => $is_followed, 'timelines' => $timelines, 'tweet_count' => $tweet_count, 'follow_count' => $follow_count, 'follower_count' => $follower_count ]); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit(User $user) { return view('users.edit', ['user' => $user]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, User $user) { $data = $request->all(); $validator = Validator::make($data, [ 'screen_name' => ['required', 'string', 'max:50', Rule::unique('users')->ignore($user->id)], 'name' => ['required', 'string', 'max:255'], 'profile_image' => ['file', 'image', 'mimes:jpeg,png,jpg', 'max:2048'], 'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->ignore($user->id)] ]); $validator->validate(); $user->updateProfile($data); return redirect('users/'.$user->id); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // } public function follow(User $user) { $follower = auth()->user(); $is_following = $follower->isFollowing($user->id); if(!$is_following) { $follower->follow($user->id); return back(); } } public function unfollow(User $user) { $follower = auth()->user(); $is_following = $follower->isFollowing($user->id); if($is_following) { $follower->unfollow($user->id); return back(); } } }
index.php
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> @foreach ($all_users as $user) <div class="card"> <div class="card-haeder p-3 w-100 d-flex"> <img src="{{ asset('storage/profile_image/' .$user->profile_image) }}" class="rounded-circle" width="50" height="50"> <div class="ml-2 d-flex flex-column"> <p class="mb-0">{{ $user->name }}</p> <a href="{{ url('users/' .$user->id) }}" class="text-secondary">{{ $user->screen_name }}</a> </div> @if (auth()->user()->isFollowed($user->id)) <div class="px-2"> <span class="px-1 bg-secondary text-light">フォローされています</span> </div> @endif <div class="d-flex justify-content-end flex-grow-1"> @if (auth()->user()->isFollowing($user->id)) <form action="{{ route('unfollow', ['id' => $user->id]) }}" method="post"> {{ csrf_field() }} {{ method_field('DELETE') }} <button type="submit" class="btn btn-danger">フォロー解除</button> </form> @else <form action="{{ route('follow', ['id' => $user->id]) }}" method="post"> {{ csrf_field() }} <button type="submit" class="btn btn-primary">フォローする</button> </form> @endif </div> </div> </div> @endforeach </div> </div> <div class="my-4 d-flex justify-content-center"> {{ $all_users->links() }} </div> </div> @endsection
回答1件
あなたの回答
tips
プレビュー