Php 回显数据(如果存在)
如果表为空,我想显示默认数据。我在laravel文档中搜索并找到了 在我的控制器中,我将数据发送到我的Php 回显数据(如果存在),php,laravel,Php,Laravel,如果表为空,我想显示默认数据。我在laravel文档中搜索并找到了 在我的控制器中,我将数据发送到我的show.blade.php public function show($id) { //Get profile data from the user $user = User::find($id); return view( 'gebruiker/show',[ 'user' => $user, ] ); } 在我的bl
show.blade.php
public function show($id)
{
//Get profile data from the user
$user = User::find($id);
return view( 'gebruiker/show',[
'user' => $user,
] );
}
在我的blade
中显示数据
@foreach ($user->profile as $profile)
<div class="col-md-12 col-xs-12" align="center">
<h1>{{ $profile->user->name or 'Geen naam' }}
</h1>
<h3>{{ $profile->age or 'Geen leeftijd' }}</h3>
<span>{{ $profile->country or 'Geen land' }}</span>
</div>
<div class="col-md-6 col-xs-6 follow line" align="center">
<h3>
Bezoekers <br/> <span>{{ $profile->visitors or 'Geen bezoekers' }}</span>
</h3>
</div>
<div class="col-md-12 col-xs-12 login_control text-center">
<br>
{{ $profile->profile or 'Geen beschrijving' }}
</div>
@endforeach
@foreach($user->profile as$profile)
{{$profile->user->name或'Geen-naam'}
{{$profile->age或'Geen-leeftijd'}
{{$profile->country或'Geen land'}
Bezoekers
{{{$profile->访客或“Geen Bezoekers”}
{{$profile->profile或'Geen beschrijving'}
@endforeach
当$profile->age
不为空时,它显示数据,但当没有数据时,它什么也不显示。我做错了什么?在blade中“或”仅在变量存在时检查。
不要尝试这样的事情:
@if(!empty($profile->age))
{{ $profile->age }}
@else
Geen leeftijd
@endif
您也可以尝试以下方法:
@if($profile->age !='' && $profile->age !='0')
{{ $profile->age }}
@else
Geen leeftijd
@endif
根据需要,您可以在模型中创建访问者:
public function getAgeAttribute($value)
{
return !empty($value) ? $value : 'Geen leeftijd';
}
理想情况下,这应该在演示者中进行。太糟糕了
{{$profile->age或'Geen-leeftijd'}
不起作用,应该这样。可能很快就会有人为此做公关。我这一行什么都没有。当我vardump$user->profile
age
时,它是空的。。。。。。。。空的??显示转储。[“age”]=>string(0)”
我不知道你是如何得到“nothing”的。为什么不呢?我的数据库中没有任何内容,那么它应该返回什么呢?是的,这很有效。。所以这是不可能的,或者(