如何在Laravel API的控制器中应用跳过和限制过滤器

时间:2017-12-08 06:01:55

标签: php laravel

## 这是我的控制器索引方法。
    如何为此方法应用过滤器。这里的bookFormat方法是获取结果的自定义方法。提前致谢。

public function index(){
        $input = Input::only('skip','limit'); 
        $books = Book::all();
        $results = [];
        foreach ($books as $book) {
         $results [] = $this->bookFormat($book);
     } 
     $skip = null;
     if(isset($input['skip'])){
        $skip = $input['skip'];
     return $results->skip($skip); 
     }
      $limit = null;
     if(isset($input['limit'])){
        $limit = $input['limit'];
        return $results->limit($limit)->get();
     }
     return $results;
 }

1 个答案:

答案 0 :(得分:0)

您可以选中import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CarGUIMethod extends JFrame { private JFrame frame; private JPanel makePanel; private JPanel modelPanel; private JPanel colorPanel; private JLabel makeLabel; private JLabel modelLabel; private JLabel colorLabel; private JComboBox make; private JComboBox model; private JComboBox color; private JButton makeBtn; private JButton modelBtn; private JButton colorBtn; public CarGUIMethod() { setTitle("Search For Cars"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 400); setLocation(300, 150); make_Panel(); add(makePanel); setVisible(true); } private void make_Panel() { String[] makes = {"Any","Acura","Audi","BMW","Ford","Ferrari","GMC","Honda", "Lamborghini","Mercedes","Nissan","Porsche","Toyota" }; makePanel = new JPanel(); makePanel.setLayout(new BoxLayout(makePanel, BoxLayout.Y_AXIS)); makePanel.add(Box.createRigidArea(new Dimension(30,30))); makeLabel = new JLabel("Select the make"); makeBtn = new JButton("OK"); make = new JComboBox<String>(makes); makeLabel.setAlignmentX(Component.LEFT_ALIGNMENT); make.setMaximumSize(make.getPreferredSize()); make.setAlignmentX(Component.LEFT_ALIGNMENT); make.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JComboBox<String> select = (JComboBox<String>)event.getSource(); String selectedMake = (String) select.getSelectedItem(); } }); makeBtn.setAlignmentX(Component.LEFT_ALIGNMENT); //makeBtn.addActionListener(new MakeButtonListener()); makePanel.add(makeLabel); makePanel.add(make); makePanel.add(makeBtn); ///// String[] models = { "Any" }; modelPanel = new JPanel(); modelPanel.setLayout(new BoxLayout(modelPanel, BoxLayout.Y_AXIS)); makePanel.add(modelPanel); modelPanel.add(Box.createRigidArea(new Dimension(30,30))); modelLabel = new JLabel("Select the model"); model = new JComboBox<String>(models); modelBtn = new JButton("OK"); model.setMaximumSize(model.getPreferredSize()); model.setAlignmentX(Component.LEFT_ALIGNMENT); modelBtn.setAlignmentX(Component.LEFT_ALIGNMENT); modelPanel.add(modelLabel); modelPanel.add(model); modelPanel.add(modelBtn); //// String[] colors = {"Any","Blue","Black","Red","Green","White","Yellow","Silver","Purple" }; colorPanel = new JPanel(); colorPanel.setLayout(new BoxLayout(colorPanel, BoxLayout.Y_AXIS)); makePanel.add(colorPanel); colorPanel.add(Box.createRigidArea(new Dimension(30,30))); colorLabel = new JLabel("Select the color"); color = new JComboBox<String>(colors); colorBtn = new JButton("OK"); colorLabel.setAlignmentX(Component.LEFT_ALIGNMENT); color.setMaximumSize(color.getPreferredSize()); color.setAlignmentX(Component.LEFT_ALIGNMENT); colorBtn.setAlignmentX(Component.LEFT_ALIGNMENT); colorPanel.add(colorLabel); colorPanel.add(color); colorPanel.add(colorBtn); } public static void main(String[] args) { new CarGUIMethod(); } } 并最后应用issetskip,take来执行此操作。

limit,offset

或者您可以使用public function index(){ $input = Input::only('skip','limit'); $query = new Book(); if(isset($input['skip']) && $input['skip']){ $query = $query->skip($input['skip']); } if(isset($input['limit']) && $input['limit']){ $query = $query->take($input['limit']); } $books = $query->get(); $results = []; foreach ($books as $book) { $results [] = $this->bookFormat($book); } return $results; } 获取Request $requestskip

limit