How to get the type of input in laravel?

by arnoldo.moen , in category: PHP Frameworks , 2 months ago

How to get the type of input in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 2 months ago

@arnoldo.moen 

In Laravel, you can get the type of input using the Request object.


Here is an example of how you can get the type of input in your controller or any other class in Laravel:

  1. Inject the Request object into your controller method or class constructor:
1
2
3
4
5
6
use IlluminateHttpRequest;

public function store(Request $request)
{
    // Your code here
}


  1. Use the input() method on the Request object to get the input value and determine its type:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public function store(Request $request)
{
    $input = $request->input('input_name');

    if (is_array($input)) {
        // Input is an array
    } elseif (is_numeric($input)) {
        // Input is a number
    } elseif (is_string($input)) {
        // Input is a string
    } else {
        // Other data types
    }
}


By using the is_array(), is_numeric(), and is_string() functions, you can determine the type of input value passed in the request.