2016-07-17 4 views
-1

테이블이 비어 있으면 기본 데이터를 표시하려고합니다. 나는 laravel 문서를 검색했고 this을 발견했다. 난 내 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 

$profile->age는 데이터를 표시하지만, 비어 있지 않은 경우 데이터가 존재하지 않는 경우 그냥 아무것도 보여주지 않는다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

"데이터 없음"을 정의하십시오. –

+0

내 행에는 아무 것도 없습니다. '$ user-> profile'''age'를 vardump 할 때 그것은 비어 있습니다. – twoam

+0

....... empty ?? 덤프 보여줘. –

답변

1

블레이드에서 "또는"변수가 존재하는 경우에만 검사합니다. 그런 까 하나이다 시도 뭔가 :

@if(!empty($profile->age)) 
    {{ $profile->age }} 
@else 
    Geen leeftijd 
@endif 
+0

그래,이 작품 .. 그래서이 또는 불가능? :( – twoam

0

당신이 시도 할 수 있습니다 : 이상적으로

public function getAgeAttribute($value) 
{ 
    return !empty($value) ? $value : 'Geen leeftijd'; 
} 

이해야

@if($profile->age !='' && $profile->age !='0') 
{{ $profile->age }} 
@else 
Geen leeftijd 
@endif 
0

를 필요에 따라이 모델의 접근을 만들 수 있습니다 발표자로 살다. 너무 좋지 않음 {{ $profile->age or 'Geen leeftijd' }}가 작동하지 않습니다. 아마 누군가는 그것을 위해 곧 홍보를 할 것입니다.