質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
GET

GETとはHTTPが対応するリクエストメソッドの一つです。クライアントからサーバーへ送られたURLパラメータのデータを取得する時必要がある時に使われます。

HTTP

HTTP(Hypertext Transfer Protocol)とはweb上でHTML等のコンテンツを交換するために使われるアプリケーション層の通信プロトコルです。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Q&A

解決済

1回答

1365閲覧

POSTがGETになってしまう[Laravel]

退会済みユーザー

退会済みユーザー

総合スコア0

GET

GETとはHTTPが対応するリクエストメソッドの一つです。クライアントからサーバーへ送られたURLパラメータのデータを取得する時必要がある時に使われます。

HTTP

HTTP(Hypertext Transfer Protocol)とはweb上でHTML等のコンテンツを交換するために使われるアプリケーション層の通信プロトコルです。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

0グッド

0クリップ

投稿2020/03/07 09:17

編集2020/03/07 09:59

参考サイト

上記記事を参考に、フォロー機能の勉強をしています。

フォロー/フォロー解除の部分で、どうしてもエラーになってしまいます。

「フォローする」ボタンを押しても、
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2020/03/07 09:28

「ご自身の」コードをご提示ください
tktcorporation

2020/03/07 09:37 編集

https://qiita.com/namizatop/items/0c81b0a94a1084cda6de#view-1 この部分ですかね ``` <form action="{{ route('follow', ['id' => $user->id]) }}" method="POST"> {{ csrf_field() }} <button type="submit" class="btn btn-primary">フォローする</button> </form> ``` POSTかGETかは、HTMLのコードで決まるので、このあたりのコードをどう書いているか、あとは、実際に生成されているHTMLも載せていただけると答えやすいです
退会済みユーザー

退会済みユーザー

2020/03/07 09:47

申し訳ありません、HTML(index.php)の中身がコントローラーになっていました。 今、修正させていただきました。
guest

回答1

0

ベストアンサー

「サーバ名/users//follow?id=8」
?id=だけでLaravel6.xのあれだな…と分かる。
https://readouble.com/laravel/6.x/ja/upgrade.html

Route::delete('users/{user}/unfollow', 'UsersController@unfollow')->name('unfollow');

{{ route('unfollow', ['id' => $user->id]) }}

ルーティングはuserなのにidで指定してるから。
5.8まではidでも問題なく動いてたけど6.0で変更というか修正?された。
ドキュメントには重要度高いようには書いてないけど実際は壮大に壊れる。

記事が間違ってるからどんなに必死に見ても意味ない。

正しくuserで指定するか、keyをなくしてもいいはず。
{{ route('unfollow', ['user' => $user->id]) }}
{{ route('unfollow', [$user->id]) }}
Userモデルそのままでも大丈夫かは自分で試して欲しい。
{{ route('unfollow', $user) }}

投稿2020/03/07 10:36

kawax

総合スコア10377

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/03/08 05:18

こちら、ありがとうございます。 いただいた内容で修正したところ、通常通り動きました。 ドキュメントの「ルートURLジェネレータと追加のパラメータ」部分を読み、 理解もできました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問