0

When I attempt to update a record in my Laravel application, it is running the wrong URL causing an error 404. This function was working fine when I was developing locally however now it is hosted on a one.com server, it has stopped working.

edit.blade.php

<form method="POST" action="gins/{{ $gins->id }}">
    @method('PATCH')
    @csrf

    <div class="field">
        <label class="label" for="gin">Gin</label>
        <div class="control">
            <input type="text" class="input" name="gin"
                   placeholder="Gin" value="{{ $gins->gin }}">
        </div>
    </div>

    <div class="field">
        <label class="label" for="size">Bottle Size(ml)</label>
        <div class="control">
            <input type="text" class="input" name="size"
                   placeholder="Size (ml)" value="{{ $gins->size }}">
        </div>
    </div>

    <div class="field">
        <label class="label" for="price">Price(£)</label>
        <div class="control">
            <input type="text" class="input" name="price"
                   placeholder="Price of Gin" value="{{ $gins->price }}">
        </div>
    </div>

    <div class="field">
        <div class="control">
            <button type="submit" class="button is-success">Update Record
            </button>
        </div>
    </div>
</form>

Route

Route::patch('gins/{gin}', 'PostsController@update')->middleware('auth');
Auth::routes(); 

Controller

public function update(Request $request, $id)
{
    $gins = \App\Gins::findOrFail($id);

    $gins->gin = request('gin');
    $gins->size = request('size');
    $gins->price = request('price');

    $gins->save();

    return redirect('gins');
}

The URL for the edit page is Laravel/gins/7/edit. When I click the submit button it's returning the URL Laravel/gins/7/gins/7 when it should be redirecting back to Laravel/gins/7.

The 7 in the Url is the record id from the particular record I'm attempting to update.

1 Answer 1

2

It's always a bad idea to hardcode urls like that. The following

<form method="POST" action="gins/{{ $gins->id }}">

in a route like laravel/gins/ would evaluate to laravel/gins/gins/7.

Also, routes change all the time in a dynamic web application. For this reason, I'd suggest you to use Named Routes. For example:

Route::patch('gins/{gin}', 'PostsController@update')
        ->middleware('auth')
        ->name('posts.update');

and then change your form action to this:

<form method="POST" action="{{ route('posts.update', ['gin' => $gins->id]) }}">

I would also clean up your update() method a bit.

public function update(Request $request, $id)
{
    $gins = \App\Gins::findOrFail($id);

    $gins->gin   = request('gin');
    $gins->size  = request('size');
    $gins->price = request('price');

    $gins->save();

    // change this to a named route as well        
    return redirect('gins');

    // or if you just want to return back to the previous page, you can do
    // return back(); 
}
Sign up to request clarification or add additional context in comments.

3 Comments

That worked great! Thankyou! Thank you for your advice regarding the hardcoding urls also. It was done this way as this is the way which was shown in the Laracasts tutorial which I have been learning from. I will use your suggested method in future though!
@Jonathan follow the Laracasts series. That is an awesome site to learn to master Laravel in the good way. Jeffrey is a great teacher and a coding ninja lol.
Good luck! Laracasts is a great resource.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.