试图获得非对象Laravel 5.4的属性

时间:2017-04-03 07:05:44

标签: php mysql laravel-5.3

你好,我总是从laravel 5.4得到一个错误。 我正在创建一个成员列表......但是当我使用@foreach时它会说

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)它不再工作...

2 个答案:

答案 0 :(得分:1)

检查此行:

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

当您使用first()时,它不会返回Std Class object。如果您需要,请使用get()

答案 1 :(得分:0)

使用first()方法,您只能从DB中获取与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>