Search code examples
laravelgetsql-updatehttp-status-code-404laravel-9

Laravel 9 404 Error on GET Request updating database when clicking button


I was having another laravel question here. I was facing 404 error when clicking the button Confirm Registration on the purpose of changing its database based on the item id on the view. Logically, I think my code should have work on this part.

<a href="{{ route('registration.confirm', $view->uniquecode) }}" class="btn btn-primary">Confirm Registration</a>

However, it comes up 404 Error. Herby I include all the related code in the section.

Error

enter image description here

Frontend (To prove that the data passing is successful in the view) enter image description here

web.php

Route::controller(ResidenceController::class)->group(function(){
    Route::get('/residence/register', 'registration')->name('residence.register');
    Route::get('/residence/search', 'SearchProperty')->name('residence.search');
    Route::get('/registration/confirmation/{$uniquecode}', 'ConfirmRegistration')->name('registration.confirm');
});

The focus is on:

Route::get('/registration/confirmation/{$uniquecode}', 'ConfirmRegistration')->name('registration.confirm');

Controller

 public function ConfirmRegistration($uniquecode){
    
            $confirm = Properties::where('uniquecode', $uniquecode)->update(['status'=>'pending', 'tenant_id'=>Auth::user()->id]);
    
            if($confirm){
                session()->flash('alert-success','Successfully Applied');
            }
            else{
                session()->flash('alert-warning','Error Occured');
            }
        }

register.blade.php

  @foreach ($toSearch as $key=>$view)
                <div class="card" style="width: 50rem;">
                    <div class="card-body">
                        <div class="container">
                            <div class="row">
                            <div class="col-md-3">
                                <img src="{{ (!empty($tenant->unit_image))? url('uploads/properties/'.$tenant->unit_image):url('uploads/properties/default.jpg') }}" class="img-fluid" alt="unit image" >
                            </div>
    
                            <div class="col-md-9">
                                <table>
                                    <tr>
                                        <td style="width: 20%">Property</td>
                                        <td style="width:10%; text-align: center">:</td>
                                        <td>{{$view->name}}</td>
                                    </tr>
    
                                    <tr>
                                        <td>Address</td>
                                        <td style="text-align: center">:</td>
                                        <td>{{$view->address1}},{{$view->address2}},{{$view->city}}</td>
                                    </tr>
    
                                    <tr>
                                        <td>Landlord</td>
                                        <td style="text-align: center">:</td>
                                        <td>{{$view->landlord->name}}</td>
                                    </tr>
    
                                    <tr>
                                        <td>Contact No</td>
                                        <td style="text-align: center">:</td>
                                        <td>{{$view->landlord->phone_number}}</td>
                                    </tr>
    
                                    <tr>
                                        <td>Status</td>
                                        <td style="text-align: center">:</td>
                                        <td>{{$view->status}}</td>
                                    </tr>
                                </table>
    
                            </div>
                        </div>
    
                    </div>
                </div>
            <br>
    
                <!--To confirm registration-->
                @if( $view->status != 'approved')
                    <a href="{{ route('registration.confirm', $view->uniquecode) }}" class="btn btn-primary">Confirm Registration</a>
                @endif
    
            </div>
    @endforeach

The focus is on:

@if( $view->status != 'approved') uniquecode) }}" class="btn btn-primary">Confirm Registration @endif

Database: enter image description here


Solution

  • you need to use post method for inserting data (register),

    anyway, change your route parameter $uniquecode to uniquecode.

    and when you use route second argument is a compact, use

    route('registration.confirm',['uniquecode' => $view->uniquecode]);