laravel类stdClass的对象无法转换为字符串

时间:2017-08-19 12:09:46

标签: php laravel

我拥有此角色的权限以及此角色所没有的其他权限我想在此角色中显示权限,并且在我的控制器中显示同一页面中的其他权限

public function index(Request $request , $id,$permission_id)
{
    $rolepermission =DB::table('permission_role')
        ->join('permissions', 'permissions.id', '=', 'permission_role.permission_id')
        ->where('permission_role.role_id' , $id)
        ->get();

    $rolepermissiongetid =DB::table('permission_role')->select('role_id')
        ->where('permission_role.role_id' , $id)
        ->get();

    $notrolepermision =DB::table('permission_role')
        ->join('permissions', 'permissions.id', '=', 'permission_role.permission_id')
        ->whereNotIn('permission_role.role_id' ,$rolepermissiongetid)
        ->get();



    $nodata = '';
    if($rolepermission==null){
        $nodata="This Roles Does Not Have Permissions";
    }

    $role=DB::table('roles')->where('id' ,$id)->first();

    return view('managements.role-permission-list')
        ->with('role' ,$role)
        ->with('data' , $rolepermission)
        ->with('role2', $notrolepermision)
        ->with('nodata' , $nodata)
        ->with('role_id' ,$id);
}

但我一直得到类stdClass的对象无法转换为字符串,

我做了foreach以在我的视图中获取数据

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            @foreach($role2 as $role2s)
                <button class="btn btn-primary">{{$role2s->name}}</button>

             @endforeach

            <!-- /.panel-heading -->
            <div class="panel-body">
        @if(isset($success_alert))
            <div class="alert alert-success">{{$success_alert}}</div>
        @endif
        @if(isset($danger_alert))
            <div class="alert alert-danger">{{$danger_alert}}</div>
        @endif


            @foreach($data as $dat)


                <div class="col-lg-3">
                    <button class="btn btn-default">{{$dat->name}}</button>
                    <hr>
                </div>


              <!--      <td>
                        <a href='/role-permission-confirm-delete/{{$dat->role_id}}/{{$dat->permission_id}}' class='btn btn-danger'><i class='fa fa-close'></i> Delete</a>

                       <!-- <a href='/role-permission/{{$dat->role_id}}/{{$dat->permission_id}}' class='btn btn-warning confirm'> View </a>
                    </td>-->

            @endforeach


        <!-- /.table-responsive -->

    </div>
        </div>
   <!-- <a href="/role-permission/create/{{$role->id}}" class="btn btn-primary">
        <i class="fa fa-plus"></i> New Permission
    </a> -->
</div>
</div>

这是我的错误

  

类stdClass的对象无法转换为字符串   在RolePermissionsController.php第32行中   at HandleExceptions-&gt; handleError('8','试图获取非对象的属性','C:\ xampp \ htdocs \ terkwazmng \ app \ Http \ Controllers \ RolePermissionsController.php','32',数组(' role_id'=&gt;'1','rolepermission'=&gt;数组(对象(stdClass),对象(stdClass),对象(stdClass),对象(stdClass),对象(stdClass),对象(stdClass),对象(stdClass) ),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass) ),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass) ),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass) ),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),ob ject(stdClass),object(stdClass),object(stdClass),object(stdClass)),'rolepermissiongetid'=&gt; array(object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass) ),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass) ),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass) ),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass),object(stdClass) ),RolePermissionsController.php第32行中的对象(stdClass),对象(stdClass),对象(stdClass),对象(stdClass),对象(stdClass),对象(stdClass),对象(stdClass))))

4 个答案:

答案 0 :(得分:1)

问题在于这段代码,$ role2s是一个对象。由于您正在尝试打印对象,因此会抛出错误

<button class="btn btn-primary">{{$role2s}}</button>

尝试在特定变量上运行var_dump。

答案 1 :(得分:0)

花括号等于PHP的echo语句。在$ role2s变量中,您将获得对象/数组,因此它不会打印但会给您一个错误。所以你可以使用以下解决方案。之后 - &gt; [&#39; &#39;] 获取它的对象或数组。

尝试

  

<强> {{DD($ role2s)}}

  

<强> {{的print_r({{$ role2s}})}}

  

<强> {{的var_dump({{$ role2s}})}}

**

答案 2 :(得分:0)

您没有指定确切的错误。但可能是错误在控制器中。检查下面的代码

$notrolepermision =DB::table('permission_role')
        ->join('permissions', 'permissions.id', '=', 'permission_role.permission_id')
        ->whereNotIn('permission_role.role_id' ,$rolepermissiongetid->role_id) //<--------------check this line you need $rolepermissiongetid->role_id in stead of $rolepermissiongetid
        ->get();

答案 3 :(得分:0)

尝试执行以下操作: 它可以100%工作

$rolepermissiongetid =DB::table('permission_role')->select('role_id')
 ->where('permission_role.role_id' , $id)
    ->get();




$arrQuery = array();
foreach($rolepermissiongetid as $key => $rolepermissiongetid){
$arrQuery[$key] = rolepermissiongetid->role_id;
 }




$notrolepermision =DB::table('permission_role')
    ->join('permissions', 'permissions.id', '=', 'permission_role.permission_id')
    ->whereNotIn('permission_role.role_id' ,$arrQuery)
    ->get();



//tunisian_developpers_team