trevorpan
4 months ago

Put search filter buttons into session and return results

Posted 4 months ago by trevorpan

May very well be butchering this process ..

The app has a filter bar with a number of drop down menus which allow users to select a job of their interest. At one point, I had a FilterJobsController but later scrapped it, as you can only get to /jobs by selecting the filter buttons - there is no /jobs link. So, then it seemed having that logic in the JobsController made good sense.

Now, I've determined that in addition to showing the job results on the app's page, I'd like the data to also be available in JSON format for use with other sites.

I've discovered the menus need to be in the session, otherwise it's very annoying to have to re-select the menu of a users choice over and over. ResourceCollections are new to me, and I'm not sure how the user selections are put into the session while retrieving the data.

Do you have any experience with this? Am I on the right track?

<?php

namespace App\Http\Controllers;

use App\Job;
use App\Http\Resources\Job as JobResource;

class JobsController extends Controller
{
    protected $job;

    public function __construct()
    {
        $this->middleware('auth')->except(['index', 'show']);
    }
    

    public function index(Request $request)
    {
        $jobs = function () {
            return JobResource::collection(Job::oldest());
        };
        
    return view('jobs.index', [
            'jobs' => Job::where([
            ['bidded', request('bidded')],
            ['state', request('state')],
            ['city', request('city')],
            ['biddertype', request('biddertype')],
            ['job', request('job')],
            ['subjob', request('subjob')], 
            ])->paginate(3)
                ->onEachSide(3)
                ->appends([
                    'bidded' => request('bidded'),
                    'state' => request('state'),
                    'city' => request('city'),
                    'biddertype' => request('biddertype'),
                    'job' => request('job'),
                    'subjob' => request('subjob'),
            ])
        ]);
    }
<?php

namespace App\Http\Resources;

use App\Job;
use App\Http\Resources\Job as JobResource;
use Illuminate\Http\Resources\Json\ResourceCollection;

class JobCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        $request->session()->put([
            'bidded' => 'bidded', 
            'state' => 'state', 
            'city' => 'city', 
            'biddertype' => 'biddertype', 
            'job' => 'job', 
            'subjob' => 'subjob'
        ]);

        return [
            'data' => $this->collection,
            'links' => [
                'self' => route('jobs.index'),
            ],
        ];
    }
}
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Job extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'jobtitle' => $this->jobtitle,
            'body' => $this->body,
            'projectaddress' => $this->projectaddress,
            'city' => $this->city,
            'state' => $this->state,
            'zipcode' => $this->zipcode,
            'biddertype' => $this->biddertype,
            'job' => $this->job,
            'subjob' => $this->subjob,
            'deadline' => $this->deadline
        ];   
    }
}

Please sign in or create an account to participate in this conversation.