首页 > 解决方案 > Laravel:“stdClass类的对象无法转换为字符串”,同时将变量传递给闭包

问题描述

我收到这样的错误 ErrorException

这是我的链接索引

<td><a href="{{route('detail_face_to_face', $tm->id)}}">{{$cmface}}</a></td> 

这是我的路线

Route::get('/detail_face_to_face/{id}', 'FaceToFaceController@detail')->name('detail_face_to_face');

这是我的控制器

public function detail($id)
    {
        $xclass = DB::table('face_to_face')->select('Class')->where('id', $id)->first();
        $tface = DB::table('tbclass')
            ->select('tbclass.F_Name','tbclass.Class')
            ->where('tbclass.Class', $xclass)
            ->whereNotExists(function ($query) use ($id) {
                $query->select('id_user')
                      ->from('absent')
                      ->where([['absent.id_face_to_face', $id],['absent.type_face_to_face', '1'],])
                      ->whereRaw('absent.id_user = tbclass.ID_No');
            })
            ->orderBy('tbclass.F_Name', 'ASC')
            ->paginate(10);
        return view('face_to_face.detail',['tface' => $tface]);
    }

这是我的 face_to_face.detail 页面

<table class="table">
   <thead style="white-space:nowrap;">
       <tr>
           <th>No</th>
           <th>Name</th>
           <th>Class</th>
       </tr>
   </thead>
   <tbody style="white-space:nowrap;">
       @if($tface->count()===0)
       <tr>
           <td class="table-success text-center" colspan="10"><< Data is Empty >></td>
       </tr>
       @else
       @foreach($tface as $no => $tm)
       <tr>
           <td>{{ ++$no + ($tface->currentPage()-1) * $tface->perPage() }}</td>
           <td>{{$tm->F_Name}}</td>
           <td>{{$tm->Class}}</td>
       </tr>
       @endforeach
       @endif
   </tbody>
</table>
{{ $tface->links() }}

如果我单击索引页面上的链接,我会收到该错误

谁能帮忙???

标签: laravellaravel-query-builderlaravel-7

解决方案


你能 dd($xclass) 吗?

错误在 ->where('tbclass.Class', $xclass)。你可能会 ->where('tbclass.Class', $xclass->Class)

看待


推荐阅读