有人知道为什么当我在视图中使用过滤结果后使用Laravel分页时出现错误吗?
它在我的简单视图中完美地工作,但是当我使用我创建的表单过滤数据时…相同的分页不起作用!
我代码:控制器:
public function filtro()
{
$keyword = Input::get('keyword');
$asignatura = Input::get('asignatura_id');
$nivel = Input::get('nivel_id');
$actividades = Actividad::where('actividad', 'LIKE', '%'.$keyword.'%');
if($asignatura){
$actividades->where('asignatura_id', $asignatura);
}
if($nivel){
$actividades->where('nivel_id', $nivel);
}
$actividads = $actividades->orderBy('created_at', 'DES')
->paginate(10);
$id_user = Sentry::getUser()->id;
$fichas = Ficha::where('user_id', $id_user)->orderBy('created_at', 'DESC')->get();
$asignatura_clave = Asignatura::where('id', $asignatura)->first();
$asignatura_id = Asignatura::orderBy('nombre', 'asc')->lists('nombre','id');
$nivel_id = Nivel::orderBy('edad', 'asc')->lists('edad','id');
$nivel_clave = Nivel::where('id', $nivel)->first();
$user = User::find($id_user);
// Devuelve todas las actividades que estan relacionadas con alguna ficha.
$actividadEnFicha = Actividad::has('fichas')
->get();
//$asignaturas = Asignatura::all();
// return var_dump($actividadEnFicha);
return View::make('actividads.index')
->with('fichas', $fichas)
->with('user', $user)
->with('palabra_clave', $keyword)
->with('keyword', $keyword)
->with('nivel_id', $nivel_id)
->with('nivel_clave', $nivel_clave)
->with('asignatura_clave', $asignatura_clave)
->with('asignatura_id', $asignatura_id)
->with('actividadEnFicha', $actividadEnFicha)
->with('actividads', $actividads);//->with('asignaturas', $asignaturas);
}
my View中的分页代码:
{{ $actividads->links(); }}
消息错误:Symfony Component HttpKernel Exception MethodNotAllowedHttpException
* @param array $others
* @return void
*
* @throws SymfonyComponentHttpKernelExceptionMethodNotAllowedHttpException
*/
protected function methodNotAllowed(array $others)
{
throw new MethodNotAllowedHttpException($others);
}
编辑:我创建了这两条路由Route::post('filtro', array('as' => 'filtro', 'uses' => 'ActividadController@filtro'));
和Route::get('filtro', array('as' => 'filtro', 'uses' => 'ActividadController@filtro'));
,并且分页工作。
现在的问题是,当我改变到另一个页面(使用分页)我的过滤器被遗忘,它再次分页我所有的活动…知道为什么吗?这是我第一次尝试,可能我用错了方法。
知道发生了什么事吗?
谢谢!
我有两个备注,首先是在if
内部,您应该分配新的结果。第二是在orderBy
中,第二个参数应该是'DESC'
而不是'DES'
。我认为第二个错误导致了你的问题。
if($asignatura){
$actividades = $actividades->where('asignatura_id', $asignatura);
}
if($nivel){
$actividades = $actividades->where('nivel_id', $nivel);
}
$actividads = $actividades->orderBy('created_at', 'DESC')
->paginate(10);
希望这能解决你的问题
编辑
MethodNotAllowedHttpException
通常是由路由不允许某些类型的请求引起的。paginate链接使用GET
将页码发送到操作,我猜你不允许在此方法的路由中使用GET
。
我已经用session
解决了分页问题。
我的问题是,当我通过分页更改页面时,所有内容都被重新加载,而我的过滤器被遗忘了。所以我为每个过滤器创建了一个session
,现在我可以通过分页来移动我的过滤数据。