【问题标题】:Laravel - Paginate and get()Laravel - 分页和获取()
【发布时间】:2013-11-06 03:48:00
【问题描述】:

使用下面的代码,我想要的是对我创建的查询进行分页。但是,当我尝试在 get 之后添加分页时,它会引发错误。我想保持 get 因为我想限制在 $fields 上设置的列。 对这件事进行分页应该是什么更好的主意?或者什么是获取和限制列的好替代品?

我尝试了什么:

->get($this->fields)->paginate($this->limit)

我的控制器的一部分:

class PhonesController extends BaseController {

    protected $limit = 5;
    protected $fields = array('Phones.*','manufacturers.name as manufacturer');
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {



        if (Request::query("str")) {
            $phones = Phone::where("model", 'LIKE', '%'. Request::query('str') . '%')
                        ->join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id')
                        ->get($this->fields);

        } else {
            $phones = Phone::join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id')
                            ->get($this->fields);
        }



        return View::make('phones.index')->with('phones', $phones);
    }

}

【问题讨论】:

    标签: php pagination laravel laravel-4


    【解决方案1】:

    如果您查看method signature,您会看到 paginate 接收到第二个参数 $columns。所以你的解决方案是使用

    ->paginate($this->limit, $this->fields);
    

    此外,您可以通过稍微改变一些东西来清理您的控制器:

    public function index()
    {
    
        $query = Phones::join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id');
    
        if ( Request::query('str') ) {
            $query->where('model', 'LIKE', '%'. Request::query('str') . '%')
        }
    
        $phones = $query->paginate($this->limit, $this->fields);
    
        return view('phones.index')->with('phones', $phones);
    }
    

    【讨论】:

      【解决方案2】:
      class Servicios extends CI_Controller
      {
      
          public function __construct()
          {
      
              parent::__construct();
      
              header('Content-Type: application/json');
              if (!$this->lib_validaciones->validarSesion(FALSE))
              {
                  exit(json_encode(array("satisfactorio" => FALSE, "mensaje" => "NO TIENE SESSION ACTIVA")));
              }
              $this->usuarioId = $this->session->userdata("usuarioId");
          }
      
          public function index()
          {
              exit();
          }
      
          public function getPremios()
          {
              $currentPage = $this->input->get("pag");
      
              \Illuminate\Pagination\Paginator::currentPageResolver(function () use ($currentPage)
              {
                  return $currentPage;
              });
      
              $this->load->model('Premio');
      
              $premios = Premio::where('activo', "TRUE")
                      ->with(['Categoria' => function($q)
                          {
                              $q->select('id', 'nombre');
                          }])
                      ->with(['Imagenes' => function ($query)
                          {
                              $query->where("activo", "TRUE");
                              $query->select(["imagenes.id", "imagenes.descripcion",
                                  new Illuminate\Database\Query\Expression(
                                          "CONCAT('" . site_url(PATH_IMAGENES_UPLOAD) . "',imagenes.id,'.',imagenes.extension) as path")
                              ]);
                          }])
                      ->with(['inventario'])
                      ->withCount(['Favoritos', 'Favoritos AS favorito_usuario' => function ($query)
                          {
                              $query->where("usuario_id", $this->usuarioId);
                          }])
                      ->orderBy("nombre")
                      ->paginate(3);
      
              $premios->setPath(site_url(uri_string()));
              $premios->setPageName("pag");
      
              exit(json_encode(array("satisfactorio" => TRUE, "premios" => $premios->toArray())));
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-05
        • 2021-09-02
        • 2018-06-04
        • 2014-12-29
        • 2021-02-09
        • 1970-01-01
        • 2017-11-02
        • 2021-03-09
        相关资源
        最近更新 更多