0
class AdminController extends Controller
{
    public function __construct() {
      $notification = Notification::where('read_status', 0)->get();
    }
}

In $notification variable constructor returns an null while data is present in notifications table.

1 Answer 1

1

Constructors do not return values, their only purpose is to instantiate class instances.

If you want to fetch data and use it in your class, you can do something like:

class AdminController extends Controller
{
    private $notifications;

    public function __construct()
    {
        $this->notifications = Notification::where('read_status', 0)->get();
    }
}

or

class AdminController extends Controller
{
    private $notifications;

    public function __construct()
    {
         $this->loadUnreadNotifications();
    }

    private function loadUnreadNotifications()
    {
        $this->notifications = Notification::where('read_status', 0)->get();
    }
}

After which you can use $this->notifications in your other controller methods.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.