Laravel分页Page2链接到空白页

时间:2019-02-13 08:33:14

标签: laravel eloquent pagination

我正在破解此Laravel查询,它对第1页的分页效果很好,但其余链接转到空白的过滤搜索页,而该页应该更多或其余查询结果。

控制器:

    <?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Job;
use Carbon\Carbon;

class FilterJobsController extends Controller
{
    /**
     * Show a list of all of the application's users.
     *
     * @return Response
     */



    public function index()
    {

        $bidded = request('bidded');
        $state = request('state');
        $city = request('city');
        $contractor = request('contractor');
        $job = request('job');
        $subjob = request('subjob');


        $jobs = DB::table('jobs')->where([
            ['bidded', '=', $bidded],
            ['state', '=', $state],
            ['city', '=', $city],
            ['contractor', '=', $contractor],
            ['job', '=', $job],
            ['subjob', '=', $subjob],
            ])->paginate(3);
        //])->get(); <<<former working method

        return view('jobs.index', compact('jobs')->with('links', $links));

    }
}
?>

刀片文件:

@extends ('layouts.master')

@section ('content')

    <div class="jobs">
        @foreach(array_chunk($jobs->getCollection()->all(), 3) as $page)
            <div class="leftarrow">❰</div> 
                @foreach ($jobs as $job) 
                   @include ('jobs.job')
                @endforeach

            <div class="pagenumbers">
                {{ $jobs->links() }}
            </div>

            <div class="rightarrow">
                <!-- <a href="{{ url('/jobs/') }}"> -->
                    ❱
                <!-- </a> -->
            </div>

            <div class="downarrowrow">
                <div class="downarrow">❱❱</div>
            </div>
        @endforeach
    </div>
@endsection

如前所述,分页页面1可以工作,但分页列表中的链接将转到空白页面,仅显示布局刀片,页眉和页脚信息,而没有进一步的搜索返回。

任何暗示为什么“链接”都没有信息,而只是有效的链接,将不胜感激。

3 个答案:

答案 0 :(得分:0)

可能由于请求数据在下一个链接上不再可用。这里提到的要求的数据是:

$bidded = request('bidded');
$state = request('state');
$city = request('city');
$contractor = request('contractor');
$job = request('job');
$subjob = request('subjob');

要解决此问题,请尝试将请求对象附加到分页响应中,如下所示:

$jobs = DB::table('jobs')->where([
   ['bidded', '=', $bidded],
   ['state', '=', $state],
   ['city', '=', $city],
   ['contractor', '=', $contractor],
   ['job', '=', $job],
   ['subjob', '=', $subjob],
])->paginate(3)->appends($request->all()); // -----> This line

答案 1 :(得分:0)

这是一个很好的提示,现在我的$ request正在使用我假定为(2)的某种多余的多余html字符来构建

这是Laravel构建的第一个有效的查询:

filterjobs?bidded=0&state=Arizona&city=Phoenix&contractor=GeneralContractor&job=Concrete&subjob=Material

(2)这是我的$ request尝试所做的事情,因此由于多余的字符以及查询的逻辑顺序似乎不合理而未返回任何内容:

filterjobs?=bidded%3D0&1=state%3DArizona&2=city%3DPhoenix&3=contractor%3DGeneralContractor&4=job%3DConcrete&5=subjob%3DMaterial&page=2

这就是我创建乱码的方式:

$request = array(
            ['bidded'.'='.$bidded)],
            ['state'. '='. $state],
            ['city'. '='. $city],
            ['contractor'. '='. $contractor],
            ['job'. '='. $job],
            ['subjob'. '='. $subjob]);

然后按照您的建议将其添加:

$jobs = DB::table('jobs')->where([
            ['bidded', '=', $bidded],
            ['state', '=', $state],
            ['city', '=', $city],
            ['contractor', '=', $contractor],
            ['job', '=', $job],
            ['subjob', '=', $subjob],
            ])->paginate(3)->appends($request);;
            // 

urlendode,urldecode或mysqi_escape_real无效。而且我以其他方式构造了该请求,但仍然会得到使查询无效的不需要的字符。而且,如果您仔细看,情况也与等号不符,出于我不太了解的原因,它似乎也弄乱了以太币中的字符串并返回了阅读困难的查询。

但似乎朝着这个方向的解决方案可能行得通,最终,至少有page2分页链接有一个查询。

答案 2 :(得分:0)

是,正确附加查询是关键,谢谢。

$this->validate(request(), [
            'bidded' => 'required',
            'state' => 'required',
            'city' => 'required',
            'contractor' => 'required',
            'job' => 'required',
            'subjob' => 'required',
        ]);


        return view('jobs.index', [
        'jobs' => Job::where([
            ['bidded', request('bidded')],
            ['state', request('state')],
            ['city', request('city')],
            ['contractor', request('contractor')],
            ['job', request('job')],
            ['subjob', request('subjob')], 
            ])->paginate(3)->appends([
                'bidded' => request('bidded'),
                'state' => request('state'),
                'city' => request('city'),
                'contractor' => request('contractor'),
                'job' => request('job'),
                'subjob' => request('subjob'),
        ])
        ]);