-1

I have a problem when trying to access any route without parameter:

When I wrte any route without {uname} parameter like this or any other one:

http://127.0.0.1:8000/login/

show me this error : enter image description here

and it is in the home in another controller?

These is my routes:

Route::get('/{uname?}', [HomeController::class, 'home'])->name('home');

Route::get('/info/{uname?}', [HomeController::class, 'info'])->name('info.me');

Route::get('/skills/{uname?}', [HomeController::class, 'skills'])->name('skills');

Route::get('/education/{uname?}', [HomeController::class, 'education'])->name('education');

Route::get('/achievements/{uname?}', [HomeController::class, 'achievements'])->name('achievements');

Route::get('/services/{uname?}', [HomeController::class, 'services'])->name('services');

Route::get('/contact/{uname?}', [HomeController::class, 'contact'])->name('contact');

Route::post('/send-email', [HomeController::class, 'sendEmail'])->name('send-email');

    Route::get('/dashboard/index', [DashboardController::class, 'index'])->name('dashboard.index');
    Route::resource('/dashboard/about', AboutController::class);
    Route::resource('/dashboard/skills', SkillsController::class);
    Route::resource('/dashboard/education', EducationController::class);

and here is my HomeController:

class HomeController extends Controller
{
    function home($uname) {
       $user = User::where('name', '=', $uname)->first();
       $about =  $user->about;
        return view('home', compact('user', 'about'));
    }


    function info($uname) {
        $user = User::where('name', '=', $uname)->first();
        $about =  $user->about;
        return view('info', compact(['user', 'about']));
    }

    function skills($uname) {
        $user = User::where('name', '=', $uname)->first();
        $about = $user->about;
        $skills = $user->skills;
        return view('skills', compact(['skills', 'user', 'about']));
    }

I have already tried those and nothing changed:

PHP artisan route: cache
PHP artisan cache:clear
Osama Mohammed
  • 2,433
  • 13
  • 29
  • 61
  • 2
    `Route::get('/{uname?}',` probably needs to be your last route as it's a catch-all type. – Marwelln Jul 17 '22 at 19:14
  • you have any information why this happened, because before there were no problem, I just add parameter and the problem appeared? by the way it is correct , and just make it as answer and I will accept it – Osama Mohammed Jul 17 '22 at 19:24
  • 1
    Your error on line 13 HomeController... can't find user with your condition...., you can change first() to firstOrFaill() method – Ali SSN Jul 17 '22 at 19:25
  • @AliSSN, thanks, worked, an also it is work when I add the home route at the end – Osama Mohammed Jul 17 '22 at 19:29
  • @AliSSN , add it as an answer – Osama Mohammed Jul 17 '22 at 19:30
  • @OsamaMohammed, also you can check isset parameters before getting user... if ( isset($uname)){ //do something} else{ //do else something} – Ali SSN Jul 17 '22 at 19:32

3 Answers3

1

Your home route is a catch-all route as you have an optional parameter right after your first dash (/). This will always catch first and stop any other routes from running because it will always match your current url. To solve this you need to put this kind of route as your last route.

As for your error it's because your not finding any user. If ->first() doesn't find a matching row it will return null, and if it's null you will get an error if you're treating it as an object. You either need to check if $user is null and set $about based on that or use firstOrFail and then create a response for that error.

Marwelln
  • 28,492
  • 21
  • 93
  • 117
1

Your error on line 13 of HomeController... can't find user with your condition and return null and you in line 14 want get about from null....

you have to choose :

1 :

  function home($uname) {
       $user  =  User::where('name', '=', $uname)->first();
       $about =  $user->about ?? null ;
        return view('home', compact('user', 'about'));
    }

2:

  function home($uname) {
       $user=$about=null;
       if(isset($uname)){
           $user  =  User::where('name', '=', $uname)->first();
           $about =  $user->about ?? null ;
       }
        return view('home', compact('user', 'about'));
    }

also you can change first() to firstOrFaill() in first method to get 404 page

Ali SSN
  • 357
  • 3
  • 6
0

$uname is an optional parameter. When it's not available no user could be found. You should check if $user is not null and return an error page or something like that, when $user is null.

if ($user !== null) {
    $about =  $user->about;
    return view('home', compact('user', 'about'));
} else {
    return view('error');
}
Sascha
  • 124
  • 5
  • but i don't use this route, why I find error related with home? for example I use login route? – Osama Mohammed Jul 17 '22 at 19:11
  • Did you define the login route? I can't see it. Looks like this route matches: Route::get('/{uname?}', [HomeController::class, 'home'])->name('home'); – Sascha Jul 17 '22 at 19:22
  • Laravel default auth routes require Auth::routes(); in the defined routes. – Sascha Jul 17 '22 at 19:24