【问题标题】:Laravel Paginator with post form data search with session storesLaravel 分页与带有会话存储的表单数据搜索
【发布时间】:2017-09-27 07:00:58
【问题描述】:

我使用 get 和 post 搜索路线。第一次用户从主页选择搜索过滤器然后它提交它点击索引控制器搜索与表单数据并返回正确的结果与分页当我点击页面时它没有显示任何东西。

Route::post('search', 'SearchController@index');
Route::get('search', 'SearchController@index');

我有索引控制器,用于第一次发布和像这样的会话。

    public function index(Request $request)
        {
           if( Request::get('select_menu') !='' ){
                $conditions = array();
                $conditions['city'] = Request::get('city');
                $conditions['area'] = Request::get('area');
                $conditions['purpose'] = Request::get('status');
                $results = Property::where($conditions)->whereBetween('price', array(1000, 10000))->paginate(6);
                Session::set('formDataSession', $conditions);
            }
        elseif(Session::has('formDataSession')){
                $getSession = Session::get('formDataSession');
                $conditions = array();
                $conditions['city'] = $getSession['city'];
                $conditions['area'] = $getSession['area'];
                $conditions['purpose'] = $getSession['purpose'];
                $results = Property::where($conditions)->whereBetween('price', array(1000, 10000))->paginate(6);

            }
    return view('search', array('page' => 'search','results'=>$results));
}

【问题讨论】:

  • 检查是否设置了会话变量,以及查询返回的内容。
  • 它返回空数据。但是当我打印会话时,我会在 ifelse 条件下获取会话中的所有数据
  • 存储时使用以下之一: $request->session()->put('key', 'value') 或 session(['key' => 'value']) 并检索与会话('键')。试试看。
  • 兄弟蒂姆,我对会话没有任何问题,它保存正确并在我们获取时返回正确的值。
  • 在结果中你得到了正确的值?然后尝试按照@Sarnodeep 的建议返回视图。

标签: php laravel laravel-5 laravel-5.3


【解决方案1】:
public function index(Request $request)
{
    $results = Property::whereBetween('price', [1000, 10000]);
    $conditions = [];

    if ($request->has('select_menu')) {
        $conditions['city'] = $request->city;
        $conditions['area'] = $request->area;
        $conditions['purpose'] = $request->status;
        session(['formDataSession' => $conditions]);
    }
    $getSession = session('formDataSession');
    $conditions['city'] = $getSession['city'];
    $conditions['area'] = $getSession['area'];
    $conditions['purpose'] = $getSession['purpose'];
    $results = $results->where($conditions)->paginate(6);

    return view('search', ['page' => 'search', 'results' => $results]);
}

【讨论】:

  • sarnodeep bro 实际上我有搜索功能,它运行两个第一个是当用户选择表单时,如果条件进入,第二次如果用户有获取请求的会话
  • Muhammad Irfan,如果 select_menu 为 null 或未设置,并且会话中没有 formDataSession 键,则搜索不会执行任何操作...这是您想要的吗?
  • 不,兄弟,我有主页,我在那里选择搜索选项,然后我这次在通话后提交表单并根据我的代码进行正确搜索,我将选择的表单值保存在会话中,但第二次点击第 2 页我达到了我的会话条件,但这次它返回空数据
  • 嘿,你介意把问题发邮件给我吗???我们可以在那里聊天... sa87207@gmail.com
  • 对不起,兄弟,我找到了解决这个问题的方法,但现在它正在工作,我遗漏了一些东西。
猜你喜欢
  • 2019-03-09
  • 2013-08-10
  • 2011-02-12
  • 1970-01-01
  • 2019-07-07
  • 2013-08-29
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多