如何清除未定义的变量Laravel 9.48.0

5sxhfpxr  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(124)
public function student(){
   $users=DB::select('select * from details');
   return view('welcome',['users'->$users]);
}

@foreach ($users as $user)
   <tr>
      <td>{{ $user->id }}</td>
      <td>{{ $user->email}}</td>
      <td>{{ $user->status}}</td>
      <td>{{ $user->subject}}</td>
   </tr>
@endforeach
yebdmbv4

yebdmbv41#

不要使用原始sql查询代替你可以这样做

public function student()
    {
        $users=Details::all();
        return view('welcome',compact('users'));
    }

或者像这样

public function student()
    {
        $users = DB::table('details')
        ->select('*')
        ->get();
        return view('welcome', compact('users'));
    }

然后在你的刀刃上你可以像这样

@isset($users)
                    @foreach ($users as $user)
                        <tr>
                            <td>{{ $user->id }}</td>
                            <td>{{ $user->email }}</td>
                            <td>{{ $user->status }}</td>
                            <td>{{ $user->subject }}</td>
                        </tr>
                    @endforeach
                @endisset

相关问题