PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 16, 2022

[FIXED] How to declare a Variable to use for all function in Laravel 6 and above laravel version?

 November 16, 2022     laravel, laravel-6, laravel-7, php     No comments   

Issue

I want to declare a variable $id

This id will be the Auth::id()

Now what will be the apporach?

Code:

private static $id;

public function __construct()
{
   $this->id = Auth::id();
}

But this id is not accessible from function Query.

Invoice::where([
     ['userID', '=', $id],
     ['created_at', '=', $todayDate]
])->get();

Giving error in this line: ['userID', '=', $id],

Undefined Variable $id.

Thanks for Advance..


Solution

public function __construct(){
    self::$id = Auth::id();
}

NOTE: If there is any static member function or variable in the class, we cannot refer it using the $this.

Just use like below

Invoice::where([
   ['userID'=>self::id],
   ['created_at'=>$todayDate]
])->get();


Answered By - A.A Noman
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing