如果用户不存在,如何从用户 table 检索用户信息并处理错误?
How can i retrieve a user information from users table and handle error if user does not exist?
我正在一个传销网站上工作,试图在 blade 视图中将用户拉入二叉树中,它适用于前三个用户,因为查询中存在此类 ID 的用户。但对于没有后代的用户,它会返回错误。
请问如何从用户 table 检索用户信息并在用户不存在时处理错误?
我的路线:
Route::get('{id}/genealogy-tree', 'UsersController@show_genealogy')->name('genealogy-tree');
我的控制器:
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;
}
我的看法:
<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>
在这里输入代码
first()
return当找不到记录时为空,您只需要在使用变量之前检查是否为空。
您可以使用 optional(..)
来避免空问题或空函数访问器 $var?->function(…)
你的控制器中的问题是因为你首先尝试找到与代码匹配 $headuser_id
的用户:
if (User::where('l_id', $headuser_id)->exists()) {
$lfu = User::where('l_id', $headuser_id)->first();
} else {
$lfu = null;
您可以将其简化为:
$lfu = User::where('l_id', $headuser_id)->first();
因为 first
将 return 用户或 null
如果他们不存在。
但是你的错误是由于代码试图使用 $lfu->id
而造成的 $lfu
它可能是 null
:
if (User::where('l_id', $lfu->id)) { ...
^^^^^^^^
您需要检查 empty($lfu)
或在 if
中分配变量,当您找到用户时,您可以使用他们的 id
搜索其他人:
$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();
}
我正在一个传销网站上工作,试图在 blade 视图中将用户拉入二叉树中,它适用于前三个用户,因为查询中存在此类 ID 的用户。但对于没有后代的用户,它会返回错误。 请问如何从用户 table 检索用户信息并在用户不存在时处理错误?
我的路线:
Route::get('{id}/genealogy-tree', 'UsersController@show_genealogy')->name('genealogy-tree');
我的控制器:
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;
}
我的看法:
<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>
在这里输入代码
first()
return当找不到记录时为空,您只需要在使用变量之前检查是否为空。
您可以使用 optional(..)
来避免空问题或空函数访问器 $var?->function(…)
你的控制器中的问题是因为你首先尝试找到与代码匹配 $headuser_id
的用户:
if (User::where('l_id', $headuser_id)->exists()) {
$lfu = User::where('l_id', $headuser_id)->first();
} else {
$lfu = null;
您可以将其简化为:
$lfu = User::where('l_id', $headuser_id)->first();
因为 first
将 return 用户或 null
如果他们不存在。
但是你的错误是由于代码试图使用 $lfu->id
而造成的 $lfu
它可能是 null
:
if (User::where('l_id', $lfu->id)) { ...
^^^^^^^^
您需要检查 empty($lfu)
或在 if
中分配变量,当您找到用户时,您可以使用他们的 id
搜索其他人:
$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();
}