#User id is added as GET request key with empty value in a POST request

1 messages · Page 1 of 1 (latest)

humble forge
#
<form action="{{ route('friends.store', $user) }}" method="POST">
    @csrf
    @method('POST')
    <button type="submit"
            class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded">
        Add friend
    </button>
</form>

when i press the button, the user ID is added to the GET parameters even though the form makes a post request. The user is also empty when i check the value inside the controller. It is just an empty User model. I have no idea what is going wrong because I have a delete form that seems to be the same form but that one works.

<form action="{{ route('friends.destroy', $user) }}" method="POST">
    @csrf
    @method('DELETE')
    <button type="submit"
            class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded">
        Remove friend
    </button>
</form>
tidal loom
#

Try removing the @method call in your first form as you don't need it, since you've set the method in the form directly

humble forge
#

hmm I still have the same problem :/

tidal loom
#

If you check your routes, is friends.store definitely set up to be a post request?

humble forge
#

yup, i have a resource controller

#

and i just tried to define the routes without resource but still doesnt work

stray zenith
#

@humble forge Show your routes.

humble forge
#
Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    Route::resource('friends', FriendsController::class)->only(['index', 'show', 'store', 'destroy']);

    Route::get('users', [UserController::class, 'index'])->name('users.index');
});
stray zenith
humble forge
#

alright i will try that, but its weird that the destroy route works anyway

#
<form action="{{ route('friends.store', ['friend' => $user]) }}"
      method="POST">
    @csrf
    <button type="submit"
            class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded">
        Add friend
    </button>
</form>
#

this should work right? because it doesnt and im not sure

stray zenith
#

Show the output of php artisan route:list --path=friends

humble forge
#

php artisan route:list --path=friends

#

oops

#

GET|HEAD friends ................. friends.index › FriendsController@index
POST friends ................. friends.store › FriendsController@store
GET|HEAD friends/{friend} .......... friends.show › FriendsController@show
DELETE friends/{friend} .... friends.destroy › FriendsController@destroy

#

i suppose POST should be friends/{friend}

#

i fixed it!!

#
Route::post('friends/{friend}', [FriendsController::class, 'store'])->name('friends.store');
Route::resource('friends', FriendsController::class)->only(['index', 'show', 'destroy']);
#

also had to run optimize

#

thanks! 🙂

stray zenith
#

Because that’s going to cache your routes.

#

Run optimize:clear and then never run it again locally.

humble forge
#

Ahh thats why i had to do that every time