undefined variable uprofile laravel 5.4

2019-06-23 17:35发布

I am using laravel framework 5.4 and write following controller class:

class ProfileController extends Controller
{
 public function index(){
      $uprofile = DB::table('user_profile')->find(Auth::id()); 
   return view('folder.profile')->with('uprofile',$uprofile);
 }

route is:

Route::get('/','ProfileController@index');

At view I'm using

@foreach($uprofile as $profile)             
    $profile->id
    @endforeach

What seems wrong here?

2条回答
Emotional °昔
2楼-- · 2019-06-23 18:26

in your index method , just remove $uprofile

return view('folder.profile')->with('uprofile');
查看更多
▲ chillily
3楼-- · 2019-06-23 18:33

Change your index() method to :

public function index(){ 
    $uprofile = DB::table('user_profile')->where('userid','=',Auth::id())->first(); 
    return view('CityOfWorks.profile')->with('uprofile',$uprofile); 
}

Also in view

@foreach($uprofile as $profile)             
    {{ $profile->id }}    // Blade views in curly braces
@endforeach
查看更多
登录 后发表回答