如何在Laravel 5中通过artisan命令调用Model?

时间:2015-04-30 09:07:45

标签: php laravel laravel-5 artisan

对于背景,我是一个laravel 5新手,但我在过去的项目中确实使用了laravel 4。

在laravel 4中,我只是在artisan命令中直接调用模型,但现在,如何在工匠命令中调用模型。这是我目前的代码:

<?php 
   namespace App\Console\Commands;
   use Illuminate\Console\Command;
   use App\Models\sampleModel as sampleModel;

   class testCommand extends Command {

  /**
  * The console command name.
  *
  * @var string
  */
   protected $name = 'test';

  /**
  * The console command description.
  *
  * @var string
  */
   protected $description = 'Command description.';

   /**
   * Create a new command instance.
   *
   * @return void
   */
    public function __construct()
    {
        parent::__construct();
    }

   /**
   * Execute the console command.
   *
   * @return mixed
   */
    public function fire()
    {
        sampleModel::sampleMethod();
    }

}

1 个答案:

答案 0 :(得分:2)

您的问题与命令无关,您可以在命令中使用Model。

问题在您的模型中,将其添加到模型的开头:

class MenuButtonTableHeaderRenderer extends JPanel implements TableCellRenderer {

    private int     column  = -1;
    private JTable  table   = null;
    private MenuButton b;

    MenuButtonTableHeaderRenderer(String name, JPopupMenu menu) {
        super(new BorderLayout());
        b = new MenuButton(ResourceManager.ARROW_BOTTOM, menu);
        b.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
        JLabel l = new JLabel(name);
        l.setFont(l.getFont().deriveFont(Font.PLAIN));
        l.setBorder(BorderFactory.createEmptyBorder(1,5,1,1));
        add(b, BorderLayout.WEST);
        add(l, BorderLayout.CENTER);
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {

        if (table != null && this.table != table) {
            this.table = table;
            final JTableHeader header = table.getTableHeader();   
            if (header != null) {   
                setForeground(header.getForeground());   
                setBackground(header.getBackground());   
                setFont(header.getFont());

                header.addMouseListener(new MouseAdapter() {

                    @Override
                    public void  mouseClicked(MouseEvent e) {
                        int col = header.getTable().columnAtPoint(e.getPoint());
                        if (col != column || col == -1) return;

                        int index = header.getColumnModel().getColumnIndexAtX(e.getPoint().x);
                        if (index == -1) return;

                        setBounds(header.getHeaderRect(index));
                        header.add(MenuButtonTableHeaderRenderer.this);
                        validate();

                        b.doClick();

                        header.remove(MenuButtonTableHeaderRenderer.this);

                        header.repaint();   
                    }
                });
            }
        }
        column = col;
        return this;
    }
}

注1,Laravel模型应以第一个资本命名:use DB; ,而不是SampleModel;

注2,sampleModel超出了已被命名为as sampleModel的模型。