PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 12, 2022

[FIXED] Laravel Search and Filter

 February 12, 2022     filter, laravel, routes, search     No comments   

Issue

This url after i am filtering data using date (wherebetween) http://127.0.0.1:8000/viewMonth/filter?_token=9jmqgiLCKNTMKtwObhlVQXCR6Vj1zcrDwsdXDDbm&dateFrom=2022-01-17&dateTo=2022-01-17

Now I am making a new function called SearchData, how i suppose to do if i want to display SearchData from the filtered date http://127.0.0.1:8000/viewMonth/filter?_token=9jmqgiLCKNTMKtwObhlVQXCR6Vj1zcrDwsdXDDbm&dateFrom=2022-01-17&dateTo=2022-01-17/{SearchData}

And this is my route

Route::get('/viewMonth/filter/search', 'App\Http\Controllers\InvoiceController@SearchReportMonth')->name('SearchReportMonth');
Route::get('/viewMonth/filter', 'App\Http\Controllers\InvoiceController@filterDateMonth')->name('filterDateMonth');

Solution

I would recommend just returning the page with the new filtered data (unless you need it to be more complex like a HTTP Request from JS for the content which I would assume you know how to handle)

an example of this is as such:

Only an example of how its done, implementations vary depending on needs

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = User::query();

        if (request('search')) {
            $query
                ->where('name', 'like', '%' . request('search') . '%')
                ->orWhere('email', 'like', '%' . request('search') . '%')
                ->orWhere('id', 'like', '%' . request('search') . '%');
        }

        if ($request->has(['field', 'sortOrder']) && $request->field != null) {
            $query->orderBy(request('field'), request('sortOrder'));
        }

        // Ignore my InertiaJS implementation, will work the same with base Blade Files.
        return Inertia::render('Users/Index', [
            'users' => fn() => $query->paginate(10)->withQueryString(),
        ]);
    }

Essentially I use request() to grab the query parameters and then construct my DB query from there and return the data it found. Hope this helps, by all means just ask if you need more of an understanding or have questions!



Answered By - MysticSeagull
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing