user13729875
user13729875

Reputation:

Undefined variable: user_id

I started a small project using Laravel and i try to pass a user_id variable from my controller to my view, but i get an error message :

Undefined variable: user_id

This is my controller function :

public function modals(Request $request){

    return view("modals.member", ["user_id" => 11]);
}

And this is my view page :

<strong>{{ $user_id }}</strong>

What wrong ?

Upvotes: 0

Views: 907

Answers (2)

Dealazer
Dealazer

Reputation: 69

You should try using the manual from Wordpress below, and correct call is, this does set even with zero value:

isset( $user->ID )

https://developer.wordpress.org/reference/functions/get_current_user_id/

This code below adds user 0 to the handle if nothing exists:

? (int) $user->ID : 0

User manual says this code which is more correct:

( isset( $user->ID ) ? (int) $user->ID : 0 );

Upvotes: 0

web tricky tips
web tricky tips

Reputation: 61

You need to use compact() as follows:

  public function modals(Request $request){
     $user_id=11;
     return view("modals.member",compact('user_id'));
  }

Upvotes: 1

Related Questions