Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Laravel request has no input

I would like to display a text only when my request does not contain any inputs

At the moment, a product list is displayed on the following page: /products. On that same page, there is a form with several inputs to filter the products.

If I filter by price, I get the following URL once the GET form is submitted: /products?price=true

I am using the following condition to check if the request has a price input: if(Input::has('price'))

This works fine, but I have several other request parameters, and I would like to display the text only when none of those inputs are filled.

This is what I have so far, it works, but it is not ideal:

@if(Input::has('price')||Input::has('mixed')||Input::has('male')||Input::has('female')||Input::has('kid')||Input::has('page'))
  //Do nothing
@else
  //Display my text here
@endif

Is there a way to do something similar but in much simpler way?

like image 553
pimarc Avatar asked Sep 04 '14 00:09

pimarc


2 Answers

Get all input data, and count it:

@if ( ! count(Input::all()))
  // Display text here
@endif
like image 78
Joseph Silber Avatar answered Sep 23 '22 14:09

Joseph Silber


@if (!request()->filled('price') || !request()->filled('mixed') || !request()->filled('male') || !request()->filled('female'))
  // Display my text here
@endif
  • Before Laravel 5.4:

    • $request->exists: Determine if the request contains a given input item key.

    • $request->has: Determine if the request contains a non-empty value for an input item.

  • In Laravel 5.5:

    • $request->exists: Alias for $request->has
    • $request->has: Determine if the request contains a given input item key.
    • $request->filled: Determine if the request contains a non-empty value for an input item.
like image 30
pimarc Avatar answered Sep 23 '22 14:09

pimarc