Home > Software engineering >  How to clear undefined variable Laravel 9.48.0
How to clear undefined variable Laravel 9.48.0

Time:01-27

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
  

CodePudding user response:

don't use raw sql query instead you can do it like this

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

or like this

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

and then in your blade you could get it like this

                @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
  • Related