0

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 ?

2
  • 2
    Welcome to SO ... perhaps there is another route handler that returns this same view that is getting hit? Commented Jun 21, 2020 at 0:45
  • 2
    Yes you alright it was another route perfect man Commented Jun 21, 2020 at 1:02

2 Answers 2

1

You need to use compact() as follows:

  public function modals(Request $request){
     $user_id=11;
     return view("modals.member",compact('user_id'));
  }
Sign up to request clarification or add additional context in comments.

Comments

0

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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.