如何将通知编号传递给视图?

时间:2016-05-26 22:16:53

标签: laravel laravel-5 laravel-5.2

我想显示新通知的数量,如下图所示。

enter image description here

根据您的理解,要显示此数字,每个操作都需要将 package swing22; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.*; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MyFrame { JFrame frame = new JFrame(" Test Frame "); JPanel panel = new JPanel(); JButton button = new JButton("CLICK"); JLabel label = new JLabel(" 33 "); public MyFrame(){ gui(); } public void gui(){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.setBackground(Color.GREEN); panel.add(button); panel.add(label); panel.add(new Surface()); frame.add(panel, BorderLayout.CENTER); button.addActionListener(new Action()); frame.pack(); frame.setSize(300,300); frame.setVisible(true); frame.setResizable(false); } class Action implements ActionListener{ @Override public void actionPerformed(ActionEvent arg0) { label.setText("new value"); } } } class Surface extends JPanel { private void doDrawing(Graphics g) { Graphics2D g2d = (Graphics2D) g; g.setColor(Color.red); g.drawOval(80, 80, 30, 30); g.fillArc(140, 140, 30, 30, 0, 90); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); doDrawing(g); } } 传递给每个视图。除了将此功能分别添加到每个操作之外,还有其他方法吗?

1 个答案:

答案 0 :(得分:0)

您应该使用视图编辑器:https://laravel.com/docs/master/views#view-composers

你的看起来可能是这样的:

public function __construct(NotificationsRepository $notifications)
{
    // Dependencies automatically resolved by service container...
    $this->notifications = $notifications;
}

public function compose(View $view)
{
    $view->with('notificationsNumber', $this->notifications()->where('read',false)->get()->count());
}

然后将其包含在服务提供商中,并将其与menu视图绑定。 Laravel将运行它,并在呈现$notificationsNumber视图的任何时候添加menu