mercredi 27 juin 2018

if else statement in the controller not responding in Laravel

am trying to prevent a logged in user from accessing some passages exception the user has his information in the NextofKin table by using this method in my controller

public function checkValidTenant($tenant_id){
    $check = NextOfKin::where('tenant_id', '=', $tenant_id)->findOrFail('tenant_id');
    return $check;
}

While in the method that will display such page, I add the following code for restriction

if($this->checkValidTenant(\Auth::user()->id)){
        $rents = RentDue::where('tenant_id', '=', \Auth::user()->id)->get();
        return view('/tenant/rent', compact('rents'));
    }else{
        return redirect()->route('tenant/profile');
    } 

I also tried it like this

if($this->checkValidTenant(\Auth::user()->id) === true){
        $rents = RentDue::where('tenant_id', '=', \Auth::user()->id)->get();
        return view('/tenant/rent', compact('rents'));
    }else{
        return redirect()->route('tenant/profile');
    }

Here is the route

 Route::get('rent', 'TenantController@rent');

The problem is when I try accessing the page with a logged user that satisfy the condition, it will throw the following error

Sorry, the page you are looking for could not be found. 

But I noticed that this page gets displayed only when I remove this restriction. How can I solve this issue?

Aucun commentaire:

Enregistrer un commentaire