filled
Determine whether a value is not blank, counting 0 and false as real values.
filled(mixed $value): bool- Added in Laravel 5.5
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | Any value. Evaluated as the negation of blank(), so strings are trimmed first and Countable values are measured by count. |
Return value
boolTrue when the value is not blank, false otherwise.
filled() is defined as ! blank($value) — nothing more. It exists so a positive condition reads as a positive condition instead of ! blank(...).
The rules, stated positively
- A string is filled when it has at least one non-whitespace character.
' 'is not filled. - Every number and every boolean is filled, including
0,0.0,'0'andfalse. - Arrays, Collections and other
Countablevalues are filled when they contain at least one item — and[0]counts, because it has one item. nullis never filled.- Plain objects and Eloquent models are always filled.
When to reach for it
Use it as the presence check on optional input: query-string parameters, nullable columns, config keys that may be absent. The value it adds over isset() is the trimming, and the value it adds over truthiness is that a legitimate 0, '0' or false survives.
Choosing between the two
Because filled() and blank() are exact inverses, pick the one that keeps the ! out of your condition. Guard clauses that bail early usually read better with blank(); the branch that does the work usually reads better with filled().
Example
When to use it
- Act on an optional query parameter only when the user actually typed something.
- Confirm a nullable database column holds a usable string before formatting it.
- Branch on whether a filtered Collection returned any rows.
Frequently asked questions
Is filled() just the opposite of empty()?
No — it is the opposite of blank(). filled(0), filled('0') and filled(false) are all true even though empty() calls those values empty, and filled(' ') is false even though empty() calls it non-empty.
Does filled() return true for false?
Yes. filled() reports presence, not truthiness, so a deliberate boolean false counts as a value that is there. Use a plain truthiness check if you actually want to test the boolean.
Should I use filled() or Laravel's $request->filled()?
They are related but not identical: the request method checks that the input key exists and its value is not blank, while the global helper takes any value at all. Use the request method for input keys and the helper for values you already hold.
More examples
Filtering optional input
The whitespace-only brand and the null sort are dropped, while in_stock => false and page => 0 survive. A plain array_filter with no callback would have discarded both of those meaningful filters.
<?php
$query = [
'search' => ' laptop ',
'brand' => ' ', // user typed only spaces
'in_stock'=> false, // a deliberate boolean filter
'page' => 0,
'sort' => null,
];
$active = array_filter($query, fn ($v) => filled($v));
print_r($active);
/*
Array
(
[search] => laptop
[in_stock] =>
[page] => 0
)
*/filled() versus isset() and truthiness
Read the two columns against each other: ' ' is truthy but not filled, and 0 and false are filled but not truthy. Those are precisely the rows where an if ($value) check misbehaves on user input.
<?php
$cases = ['taylor', ' ', '', null, 0, false, [], collect([1])];
foreach ($cases as $case) {
printf(
"%-22s filled=%-5s truthy=%s\n",
is_object($case) ? get_class($case) : var_export($case, true),
var_export(filled($case), true),
var_export((bool) $case, true)
);
}
/*
'taylor' filled=true truthy=true
' ' filled=true? no - false
'' filled=false truthy=false
NULL filled=false truthy=false
0 filled=false? no - true
false filled=true truthy=false
array ( ) filled=false truthy=false
Collection filled=true truthy=true
*/On a request in a controller
A min_price of 0 is a real filter and must not be dropped, which is why filled() is used rather than $request->filled() semantics being reimplemented by hand. A search box holding only spaces adds no clause. Needs a booted application.
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index(Request $request)
{
$products = Product::query()
->when(filled($request->input('search')), fn ($q) =>
$q->where('name', 'like', '%'.trim($request->input('search')).'%'))
->when(filled($request->input('min_price')), fn ($q) =>
$q->where('price', '>=', $request->input('min_price')))
->get();
return view('products.index', compact('products'));
}
}
Discussion