Home > Software design >  How can i retrieve a user information from users table and handle error if user does not exist?
How can i retrieve a user information from users table and handle error if user does not exist?

Time:10-11

I am working on an MLM website trying to pull the users in a binary TREE in the blade view, it works for the first Three users because the user where such ID in query exists. but for users who don't have descendants it throws back error. Please how can i retrieve a user information from users table and handle error if user does not exist?

my route:

Route::get('{id}/genealogy-tree', 'UsersController@show_genealogy')->name('genealogy-tree');

my controller:

public function show_genealogy($id)
{
    // $headuser = User::findOrFail($id);
    // $headuser_id = $headuser->id;
    if (User::findOrFail($id)->exists()) {
        $headuser = User::findOrFail($id);
        $headuser_id = $headuser->id;
    } else {
        $headuser = null;
        $headuser_id = null;

    }
    #-------------------------------------------------------
    if (User::where('l_id',  $headuser_id)->exists()) {
        $lfu = User::where('l_id',  $headuser_id)->first();
    } else {
        $lfu = null;
    }
    if (User::where('r_id',  $headuser_id)->exists()) {
        $rfu = User::where('r_id',  $headuser_id)->first();
    } else {
        $rfu = null;
    }

    // return view('users.genealogy-tree', compact('headuser','lfu','rfu'));
    #--------------------------------------------------------
    if (User::where('l_id',  $lfu->id)) {
        $lsu = User::where('l_id',  $lfu->id)->first();
    } else {
        $lsu = null;
    }       
    
    if (User::where('r_id',  $lfu->id)->exists()) {
        $rsu = User::where('r_id', $lfu->id)->first();
    }  else {
        $rsu = null;
    } 

    if (User::where('l_id',  $rfu->id)->exists()) {
        $ltu = User::where('l_id',  $rfu->id)->first();
    } else {
        $ltu = null;
    } 
    if (User::where('r_id',  $rfu->id)->exists()) {
        $rtu = User::where('r_id',  $rfu->id)->first();
    } else {
        $rtu = null;
    } 
    #--------------------------------------------------------

    return view('users.genealogy-tree', compact('headuser','lfu','rfu','lsu','rsu','ltu','rtu'));
    // return $lfu;
}

my view:

<div class="table-responsive">
<table class="table" style="align:center; border:o; text-align:center">
    <tr height="150" >
        <td></td>
        @isset($headuser)
        <td colspan="6"> <a href="{{route('genealogy-tree', $headuser->id)}}"><i class="fa fa-user fa-4x" style="color: #45fc03 !important; font-size: 1000%;"></i>
        <div class="tree_div"> 
        <p>User Name: {{$headuser->name ? $headuser->name : 'No Record'}}</p>
        <p>Network Worth: {{$headuser->network_bonus ? $headuser->network_bonus : 'No Record'}}</p>
        <p>Donations: {{$headuser->donation_sum ? $headuser->donation_sum : 'No Record'}}</p>
        <p>Current Level: {{$headuser->level ? $headuser->level : 'No Record'}}</p>     
        </div></a>
        </td>
        @else
        <td colspan="6"> <a href="#"><i class="fa fa-user fa-4x" style="color: gray !important; font-size: 1000%;"></i>
        <div class="tree_div">
            <br> 
        <p>{{'No User'}}</p>   
        </div></a>
        @endisset
        <td></td>
    </tr>

    <tr height="150">
        @isset($lfu)
        <td colspan="4"> <a href="{{route('genealogy-tree', $lfu->id)}}"><i class="fa fa-user fa-4x" style="color: #b6fc03 !important; font-size: 700%;"></i>
        <div class="tree_div"> 
        <p>User Name: {{$lfu->name ? $lfu->name : 'No Record'}}</p>
        <p>Network Worth: {{$lfu->network_bonus ? $lfu->network_bonus : 'No Record'}}</p>
        <p>Donations: {{$lfu->donation_sum ? $lfu->donation_sum : 'No Record'}}</p>
        <p>Current Level: {{$lfu->level ? $lfu->level : 'No Record'}}</p>     
        </div></a>
        </td>
        @else
        <td colspan="4"> <a href="#"><i class="fa fa-user fa-4x" style="color: gray !important; font-size: 700%;"></i>
        <div class="tree_div">
            <br> 
        <p>{{'No User'}}</p>   
        </div></a>
        @endisset
        @isset($rfu)
        <td colspan="4"> <a href="{{route('genealogy-tree', $rfu->id)}}"><i class="fa fa-user fa-4x" style="color: #b6fc03 !important; font-size: 700%;"></i>
        <div class="tree_div"> 
        <p>User Name: {{$rfu->name ? $rfu->name : 'No Record'}}</p>
        <p>Network Worth: {{$rfu->network_bonus ? $rfu->network_bonus : 'No Record'}}</p>
        <p>Donations: {{$rfu->donation_sum ? $rfu->donation_sum : 'No Record'}}</p>
        <p>Current Level: {{$rfu->level ? $rfu->level : 'No Record'}}</p>     
        </div></a>
        </td>
        @else
        <td colspan="4"> <a href="#"><i class="fa fa-user fa-4x" style="color: gray !important; font-size: 700%;"></i>
        <div class="tree_div">
            <br> 
        <p>{{'No User'}}</p>   
        </div></a>
        @endisset
    </tr>

     <tr height="150" style="color: #f4fc03; font-size: 130%;">
       
        @isset($lsu)
        <td colspan="2"> <a href="{{route('genealogy-tree', $lsu->id)}}"><i class="fa fa-user fa-4x" style="color: #f4fc03 !important;"></i>
        <div class="tree_div"> 
        <p>User Name: {{$lsu->name ? $lsu->name : 'No Record'}}</p>
        <p>Network Worth: {{$lfu->network_bonus ? $lfu->network_bonus : 'No Record'}}</p>
        <p>Donations: {{$lsu->donation_sum ? $lsu->donation_sum : 'No Record'}}</p>
        <p>Current Level: {{$lsu->level ? $lsu->level : 'No Record'}}</p>     
        </div></a>
        </td>
        @else
        <td colspan="2"> <a href="#"><i class="fa fa-user fa-4x" style="color: gray !important; font-size: 700%;"></i>
        <div class="tree_div">
            <br> 
        <p>{{'No User'}}</p>   
        </div></a>
        @endisset
        @isset($rsu)
        <td colspan="2"> <a href="{{route('genealogy-tree', $rsu->id)}}"><i class="fa fa-user fa-4x" style="color: #b6fc03 !important;"></i>
        <div class="tree_div"> 
        <p>User Name: {{$rsu->name ? $rsu->name : 'No Record'}}</p>
        <p>Network Worth: {{$rsu->network_bonus ? $rsu->network_bonus : 'No Record'}}</p>
        <p>Donations: {{$rsu->donation_sum ? $rsu->donation_sum : 'No Record'}}</p>
        <p>Current Level: {{$rsu->level ? $rsu->level : 'No Record'}}</p>     
        </div></a>
        </td>
        @else
        <td colspan="2"> <a href="#"><i class="fa fa-user fa-4x" style="color: gray !important; font-size: 700%;"></i>
        <div class="tree_div">
            <br> 
        <p>{{'No User'}}</p>   
        </div></a>
        @endisset

        @isset($ltu)
        <td colspan="2"> <a href="{{route('genealogy-tree', $ltu->id)}}"><i class="fa fa-user fa-4x" style="color: #f4fc03 !important;"></i>
        <div class="tree_div"> 
        <p>User Name: {{$ltu->name ? $ltu->name : 'No Record'}}</p>
        <p>Network Worth: {{$ltu->network_bonus ? $ltu->network_bonus : 'No Record'}}</p>
        <p>Donations: {{$ltu->donation_sum ? $ltu->donation_sum : 'No Record'}}</p>
        <p>Current Level: {{$ltu->level ? $ltu->level : 'No Record'}}</p>     
        </div></a>
        </td>
        @else
        <td colspan="2"> <a href="#"><i class="fa fa-user fa-4x" style="color: gray !important; font-size: 700%;"></i>
        <div class="tree_div">
            <br> 
        <p>{{'No User'}}</p>   
        </div></a>
        @endisset
        @isset($rtu)
        <td colspan="2"> <a href="{{route('genealogy-tree', $rtu->id)}}"><i class="fa fa-user fa-4x" style="color: #b6fc03 !important;"></i>
        <div class="tree_div"> 
        <p>User Name: {{$rtu->name ? $rtu->name : 'No Record'}}</p>
        <p>Network Worth: {{$rtu->network_bonus ? $rtu->network_bonus : 'No Record'}}</p>
        <p>Donations: {{$rtu->donation_sum ? $rtu->donation_sum : 'No Record'}}</p>
        <p>Current Level: {{$rtu->level ? $rtu->level : 'No Record'}}</p>     
        </div></a>
        </td>
        @else
        <td colspan="2"> <a href="#"><i class="fa fa-user fa-4x" style="color: gray !important; font-size: 700%;"></i>
        <div class="tree_div">
            <br> 
        <p>{{'No User'}}</p>   
        </div></a>
        @endisset

    </tr>
   


    <!-- <tr height="150" style="color: #fcb603; font-size: 100%;">
        <td> <i class="fa fa-user fa-4x"></i></td>
        <td> <i class="fa fa-user fa-4x"></i></td>
        <td> <i class="fa fa-user fa-4x"></i></td>
        <td> <i class="fa fa-user fa-4x"></i></td>
        <td> <i class="fa fa-user fa-4x"></i></td>
        <td> <i class="fa fa-user fa-4x"></i></td>
        <td> <i class="fa fa-user fa-4x"></i></td>
        <td> <i class="fa fa-user fa-4x"></i></td>
    </tr> -->
    

</table>
enter code here

CodePudding user response:

first() returns null when a record is not found, you just need to check for null before using the variable.

You can use optional(..) to avoid null issues or the null function accessor $var?->function(…)

The problem in your controller is because you first try to find the user matching $headuser_id with the code:

if (User::where('l_id',  $headuser_id)->exists()) {
    $lfu = User::where('l_id',  $headuser_id)->first();
} else {
    $lfu = null;

You can simplify this to:

$lfu = User::where('l_id',  $headuser_id)->first();

As first will return the User or null if they don't exist.

But your error is caused by the code trying to use $lfu->id when $lfu it might be null:

if (User::where('l_id',  $lfu->id)) { ...
                         ^^^^^^^^

You either need to check for empty($lfu) or assign the variable in an if, when you find the user you can the use their id to search for the others:

$lsu = null;
$ltu = null;

if ($lfu = User::where('l_id',  $headuser_id)->first()) {
    $lsu = User::where('l_id',  $lfu->id)->first();
    $ltu = User::where('l_id',  $rfu->id)->first();
}
  • Related