Default Input Value in Laravel

3

In%%, I want, when a given value does not exist in Laravel , I can set a default value.

With pure PHP, you can do this:

$text = isset($_POST['text']) ? $_POST['text'] : 'padrão';

In Laravel I know it's possible to do something like this:

 $text = Input::has('text') ? Input::get('text') : 'padrão';

Given that Input is an excellent framework with some features that slow down continuous code rewriting, is there an easier way to get a stop value for an input value? Or is this the only way out?

Sometimes it gets tiresome to do:

$a = Input::has('a') ? Input::get('a') : 'Valor padrão';
$b = Input::has('b') ? Input::get('b') : 'Valor padrão';
$c = Input::has('c') ? Input::get('c') : 'Valor padrão';
    
asked by anonymous 15.03.2016 / 16:47

2 answers

3

You can use Input::get('nome_campo','valor'); in this case if the second parameter identifies the default value if the Input returns null.

    
15.03.2016 / 17:10
2

Laravel includes a helper that does just that:

array_get($array, 'campo', 'valor padrão'); // null como default

The advantage is that you can do this anywhere, not necessarily only in input .

In particular, I do not like array_get , I'm the Null coalescing operator of PHP 7.

    
20.03.2016 / 02:47