What do you get if you:
@php dd($testresults); @endphp
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I know i'm a beginner when it comes to Laravel, but i've been googling endlessly for over a week to try and figure out what i'm doing wrong. I can't even seem to copy and paste to get it right!
Basically I have my controller:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
class TestResultsController extends Controller
{
/**
* Show a list of all of the application's pol test results.
*/
public function testresults()
{
// $testresults ='empty';
$testresults =DB::select('select * from tests');
return view('tests',compact('testresults'));
}
}
I have my Route:
Route::get('/tests', [TestResultsController::class, 'testresults']);
and then in my blade file called tests.blade.php
{{$testsresults}}
</span>
I figure the issue is in the controller and i've tried so many variations i'm now totally lost. It's laravel 11. It's annoying me because I have used Laravel last year even if I copy bits from that I can't get it to work! The rest of the pages work just not the ones I try and call variables in.
Thank you for your help!
@Glens Like @snapey says, your controller will never ever be hit.
The order or the routes is the key here.
Take a look here, at a simplification of your routes.
Route::get('/{any}', [RoutingController::class, 'root'])->name('any);
Route::get('/pooltests', [PoolTestResultsController::class, 'pooltestresults' ] )->name('pooltests');
The first one will always be the one matching, it means /whatever-you-put-here-will-match-including-pooltests
Of course if you are not logged in then it hits the correct route, or at least should.
You need to be careful with the order of the routes when it comes to using wildcards in them.
So this
Route::group(['prefix' => '/', 'middleware' => 'auth'], function () {
Route::get('', [RoutingController::class, 'index'])->name('root');
Route::get('/home', fn()=>view('dashboards.index'))->name('home');
Route::get('{first}/{second}/{third}', [RoutingController::class, 'thirdLevel'])->name('third');
Route::get('{first}/{second}', [RoutingController::class, 'secondLevel'])->name('second');
Route::get('{any}', [RoutingController::class, 'root'])->name('any');
});
Route::get('/pooltests', [PoolTestResultsController::class, 'pooltestresults' ] )->name('pooltests');
Should be this, that way it will hit the pooltests and not the any route.
Route::get('/pooltests', [PoolTestResultsController::class, 'pooltestresults' ] )->name('pooltests');
Route::group(['prefix' => '/', 'middleware' => 'auth'], function () {
Route::get('', [RoutingController::class, 'index'])->name('root');
Route::get('/home', fn()=>view('dashboards.index'))->name('home');
Route::get('{first}/{second}/{third}', [RoutingController::class, 'thirdLevel'])->name('third');
Route::get('{first}/{second}', [RoutingController::class, 'secondLevel'])->name('second');
Route::get('{any}', [RoutingController::class, 'root'])->name('any');
});
Then of course you have some other errors in your routes file, and you are using a very bad practice when it comes to how you define your routes and controllers.
I highly recommend starting here before you do anything else.
Please or to participate in this conversation.