0
votes

I'm hacking this Laravel query, it returns this pagination fine for page 1, but the rest of the links go to blank filtered-search pages which should be more or the rest of the query results.

Controller:

    <?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));

    }
}
?>

Blade file:

@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

As stated, the pagination page 1 work but the links in the paginated list go to a blank page, just layout-blade, header and footer info, no furthered search returns.

Any hints as to why the "links" has no information, just a valid link, would be appreciated.

3

3 Answers

0
votes

It might due the request data no longer available on the next link. The requested data mentioned here are:

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

To cater this issue, try append the request object into paginate response as following:

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

That is a goodtip, now my $request is being built with some kind of unwanted extra html characters I assume (2)

This is the first query Laravel built which works:

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

(2) Here is what my $request attempt is doing, thus returning nothing because of the extra characters and a seemingly wacked out logic order for the query:

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

This is how I built that garble:

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

And then appeneded it as you suggested:

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

urlendode, urldecode, or mysqi_escape_real does not help. And I have constructed that request in other ways but still get the unwanted characters which make the query invalid. And, if you look carefully, things are also out of order with the equal signs, it seems to have also juggled the string in the ether returning some dyslexic query for reasons I do not quite understand.

But it seems a solution in this direction may work, eventually, atleast page2 pagination link has a query.

0
votes

Yes appending the query properly was the key, thanks.

$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'),
        ])
        ]);