2017-04-03 72 views
2

嗨Guy的我總是從laravel 5.4中得到一個錯誤。 林創建成員的名單...但是當我使用@foreach它說試圖獲得非對象Laravel的屬性5.4

ErrorException in 6163a8b030c7474bc8eaad359ab99eb61ebdb127.php line 38: 
Trying to get property of non-object (View: E:\wamp64\www\gplspring2017\resources\views\admin\memberlist.blade.php) 

這裏是我的控制器

public function listmember(Request $request, $idteam) 
{ 
    $teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->first(); 
    $count = count($teams); 

    if (!$count) { 
     return redirect('404'); 
    } else { 
     return view('/admin/memberlist', ['team' => $teams]); 
    } 
} 

這是我的看法代碼:

<table class="table table-striped"> 
    <tr> 
     <td style="width:15%;"></td> 
     <td style="width:25%;">Summoners Name</td> 
     <td style="width:25%;">Name</td> 
     <td style="width:25%;">Role</td> 
     <td style="width:10%;"></td> 
    </tr> 
    <?php 
    $qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first(); 

    $counting = count($qmember); 

    ?> 
    @if (! $counting) 
    <tr> 
     <td colspan="4"> No Recored! </td> 
    </tr> 
    @else 
    @foreach($qmember as $get_member) 
    <tr> 
     <td><img src="{{ $get_member->member_pic }}" /></td> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td> 
      <a href="" data-toggle="tooltip" title="Edit Team"><div class="glyphicon glyphicon-pencil"></div></a> | 
      <a href="" data-toggle="tooltip" title="Delete Team"><div class="glyphicon glyphicon-trash"></div></a> 
     </td> 
    </tr> 
    @endforeach 
    @endif 
</table> 

當我刪除代碼工作的foreach ..但我嘗試添加@foreach($qmember as $get_member)它不工作了...

+1

第一個問題是你的foreach裏面的變量必須'$ team'和第二件事是你只用從數據庫讀取一個記錄第一()方法不是全部匹配的記錄。所以使用get()而不是第一個() – webDev

+0

感謝兄弟...現在它工作... – ivor

回答

1

檢查這一行:

$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first(); 

當您使用first()那麼它不會返回Std Class object。如果你想這樣,然後使用get()

+0

謝謝...它幫助我... – ivor

+0

聽起來不錯! –

0

使用first()方法,你只是從數據庫中取得一個與ID匹配的記錄(第一個匹配)。你需要使用get()。

控制器:

public function listmember(Request $request, $idteam) 
    { 

     $teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->get(); //Use get() if you expect more than one result not first() 
     $count = count($teams); 

     if(! $count) 
     { 
      return redirect('404'); 
     } 
     else 
     { 
      return view('/admin/memberlist', ['teams' => $teams]);// this team variable must be used in the blade 
     } 
    } 

查看:

<table class="table table-striped"> 
    <tr> 
     <td style="width:15%;"></td> 
     <td style="width:25%;">Summoners Name</td> 
     <td style="width:25%;">Name</td> 
     <td style="width:25%;">Role</td> 
     <td style="width:10%;"></td> 
    </tr> 
    <?php 

     $qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first(); 

     $counting = count($qmember); 
    ?> 
    @if (! $counting) 
    <tr> 
     <td colspan="4"> No Recored! </td> 
    </tr> 
    @else 
     @foreach($teams as $team) 
      <tr> 
       <td><img src="{{ $team['member_pic'] }}" /></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td> 
        <a href="" data-toggle="tooltip" title="Edit Team"><div class="glyphicon glyphicon-pencil"></div></a> | 
        <a href="" data-toggle="tooltip" title="Delete Team"><div class="glyphicon glyphicon-trash"></div></a> 
       </td> 
      </tr> 
     @endforeach 
    @endif 
</table> 
相關問題