murilo
278
7
Laravel

GETTING Query Values from Constructor

Posted 11 months ago by murilo

Hello Guys , I have a question . If I am trying to add the variables of my search directly in the construct , but I dont know if I can put this value directly like this .

it not aloud I add this value like this -

use ($this->name)

LIKE THIS -


    public function __construct(Request $request)
    {

        $this->name = 'John';

    }
   public function getValue() 
  {
       $user = User::whereHas('UserInfo', function($q) use ($this->name) {
            $q->where('name_user', $this->name);
        })->get();
   }

I have to do like this -


    public function __construct(Request $request)
    {

        $this->name = 'John';

    }
   public function getValue() 
  {
    $name = $this->name;
       $user = User::whereHas('UserInfo', function($q) use ($name) {
            $q->where('name_user', $name);
        })->get();
   }

Does some one knows If it has any way to to add this value getting directly from constructor ? something like this -

User::whereHas('UserInfo', function($q) use ($this->name)  { ... 

Thanks

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.