ethar's avatar
Level 5

Multi Parameter in Search 'GET'

i try to make multi search parameter, i check if request->any have value or not, if have value shot match that value , if not should not append it in search

public function search(Request $request)
    {
        $product = Products::select('id', 'cats_id', 'brands_id', 'name_ar', 'admins_id')
            ->with('admins:id,name')
            ->with('cats:id,cats_name_ar')
            ->with('brands:id,brands_name_ar')
            ->paginate(10);
        if ($request->s !== null) {
            $product->where('name_ar', 'LIKE', "%{$request->s}%");
        }
        if ($request->discount == '1') {
            $product->where('discount', '!=', null);
        }
        return view('admin.product.indexproduct', compact('product'));
    }

but not worked, all value returned, my condition ignored, plz how to do that.

0 likes
4 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

Append the parameters to pagination links:

From docs:

Appending To Pagination Links

You may append to the query string of pagination links using the appends method. For example, to append sort=votes to each pagination link, you should make the following call to appends:

{{ $users->appends(['sort' => 'votes'])->links() }}

If you wish to append all current query string values to the pagination links you may use the withQueryString method:

{{ $users->withQueryString()->links() }}

Also try moving your paginate to just before return view:

$product->paginate(10);

Example:

       $query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);

       $params = array('psch' => $dogsearch, 'aval' => $aval);

And view:

{{ $dogs->appends($params)->links() }} 
1 like
ethar's avatar
Level 5

my problem not in pagination, my problem how to get data from the database depend on the query parameter, the query parameter may be sent or may be null.

jlrdw's avatar

Just like I did here:

        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }

You have to work it out in some if statements while building up the query.

First pass is a post from a search form, all other passes is a get request.

Aren't you retrieving the parameters from the request, if not, use the network tab in your browser developer tools and see what request and response you are getting.

Did you move paginate, it can go last after all other parts are built up.

And example:

        If (!empty(Request::input('psch'))) {
            $dogsearch = Request::input('psch');
        } else {
            $dogsearch = "";
        }


In your case check for null, and you can use a ternary if also.

public function search(Request $request)
    {
        $query = Products::select('id', 'cats_id', 'brands_id', 'name_ar', 'admins_id')
            ->with('admins:id,name')
            ->with('cats:id,cats_name_ar')
            ->with('brands:id,brands_name_ar');
        if ($request->s !== null) {
            $query->where('name_ar', 'LIKE', "%{$request->s}%");
        }
        if ($request->discount == '1') {
            $query->where('discount', '!=', null);
        }
	$product = $query->paginate(10);
	
	// if sending parameters as array
	$params = [your params array];
	return view('admin.product.indexproduct', compact('product', 'params'));  // if sending params
        return view('admin.product.indexproduct', compact('product'));  //if not sending params
    }


ethar's avatar
Level 5

@jlrdw thx pro my last code


    public function search(Request $request){
        $search = Products::select('id', 'cats_id', 'brands_id', 'name_ar', 'admins_id')
            ->with('admins:id,name')
            ->with('cats:id,cats_name_ar')
            ->with('brands:id,brands_name_ar');

        if($request->discount == 1) $search->where('discount', '!=' , null) ? : '';

        $product=$search->paginate(10);
        return view ('admin.product.indexproduct',compact('product'));
    }

Please or to participate in this conversation.