Skip to content
Advertisement

routes/web route me to another route

My web doesn’t seem to be directing to the correct page.

Here is my blade

<div class="container">
    <div class="col-lg-12 d-flex justify-content-between align-items-center">
        <h2>Informations</h2>
        <a href="{{ route('add-new-information') }}" class="btn text-success">
            <i class="fas fa-plus-circle fa-2x"></i>
        </a>
    </div>
    <hr>
    <div class="row d-flex justify-content-center align-items-center mt-5" style="max-height: 500px !important; overflow-y: scroll">
        @foreach ($informations as $info)
            <div class="card col-sm-11 p-0 mb-4 clickable-item" onclick='window.location = "{{ route('admin-informations', ['id' => $info->id]) }}"'>
                ...
            </div>
        @endforeach
    </div>
</div>

Here is my routes/web

Auth::routes();

Route::group(['middleware' => ['auth'], 'prefix' => '/',], function () {
    Route::get('/', function () {
        return redirect('/home');
    });

    Route::group(['prefix' => 'admin'], function() {
        Route::get('/informations', [AppHttpControllersInformationController::class, 'index'])->name('informations');
        Route::get('/informations/{id}', [AppHttpControllersInformationController::class, 'indexAdminInfo'])->name('admin-informations');
        Route::get('/informations/add-new-information', [AppHttpControllersInformationController::class, 'add'])->name('add-new-information');
    });

});

and here is my controller

    public function indexAdminInfo($id){
        $information = Information::find($id);
        // $comments = Comment::where('information_id', $id)->orderByDesc('created_at')->get();
        $ack_count = Acknowledge::where('information_id', $id)->count();
        $user_ack = Acknowledge::where([
            ['information_id', '=', $id],
            ['user_id', '=', Auth::user()->id],
        ])->first();
        $ack = 'FALSE';
        
        if($user_ack != null){
            $ack = 'TRUE';
        }

        return view('adminviews/infoadmin', compact('information', 'ack_count', 'ack', 'user_ack'));
    }

    public function add(){
        return view('adminviews/addinfo');
    }

For some reason, when I click the a tag with the href {{ route('add-new-information') }} to go to the add page 'adminviews/addinfo',

instead the page will go to the 'adminviews/infoadmin' page, which will cause an error, because no parameters are being sent.

I tried checking the code, but it looks correct to me. Can anybody find an error on this?

Advertisement

Answer

the problem is with your routes: these two routes are ambiguous:

Route::get('/informations/{id}');
Route::get('/informations/add-new-information');

just think of below scenario:

router wants to route, this url : /information/add-new-information

router will hit the first defined route, because it is compatible with the definition ('/informations/{id}')

Note :{id} is a variable and can be any string

so it will go with this.

Solution

write the more restricted route first,

and more general route later:

Route::get('/informations/add-new-information');
Route::get('/informations/{id}');
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement