Home > database >  routes/web route me to another route
routes/web route me to another route

Time:11-19

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') }}" hljs-string">">
            <i hljs-number">2x"></i>
        </a>
    </div>
    <hr>
    <div hljs-number">5" style="max-height: 500px !important; overflow-y: scroll">
        @foreach ($informations as $info)
            <div hljs-number">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', [App\Http\Controllers\InformationController::class, 'index'])->name('informations');
        Route::get('/informations/{id}', [App\Http\Controllers\InformationController::class, 'indexAdminInfo'])->name('admin-informations');
        Route::get('/informations/add-new-information', [App\Http\Controllers\InformationController::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?

CodePudding user response:

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}');
  • Related