为什么构造函数 returns 在 Laravel 5.6 中为 null
Why constructor returns a null in Laravel 5.6
class AdminController extends Controller
{
public function __construct() {
$notification = Notification::where('read_status', 0)->get();
}
}
在 $notification
变量构造函数中 returns 一个 null
而通知中存在数据 table。
构造函数没有 return 个值,它们的唯一目的是实例化 class 个实例。
如果您想获取数据并在您的 class 中使用它,您可以这样做:
class AdminController extends Controller
{
private $notifications;
public function __construct()
{
$this->notifications = Notification::where('read_status', 0)->get();
}
}
或
class AdminController extends Controller
{
private $notifications;
public function __construct()
{
$this->loadUnreadNotifications();
}
private function loadUnreadNotifications()
{
$this->notifications = Notification::where('read_status', 0)->get();
}
}
之后您可以在其他控制器方法中使用 $this->notifications
。
class AdminController extends Controller
{
public function __construct() {
$notification = Notification::where('read_status', 0)->get();
}
}
在 $notification
变量构造函数中 returns 一个 null
而通知中存在数据 table。
构造函数没有 return 个值,它们的唯一目的是实例化 class 个实例。
如果您想获取数据并在您的 class 中使用它,您可以这样做:
class AdminController extends Controller
{
private $notifications;
public function __construct()
{
$this->notifications = Notification::where('read_status', 0)->get();
}
}
或
class AdminController extends Controller
{
private $notifications;
public function __construct()
{
$this->loadUnreadNotifications();
}
private function loadUnreadNotifications()
{
$this->notifications = Notification::where('read_status', 0)->get();
}
}
之后您可以在其他控制器方法中使用 $this->notifications
。