Why don't we use Fluent Class with our private data, despite its power? I found this source https://www.youtube.com/watch?v=8PqgRoCe0N0 that explains some of its features, including in my opinion, it does not show an error message when there is no specific element and null appears instead
use Illuminate\Support\Fluent;
$data = [
'a' => 1,
'b' => 2,
];
dd($data['c']); // Undefined array key "c"
$fluent = new Fluent($data);
dd($fluent['c']); // null
dd($fluent['a']); // 1
dd($fluent->b); // 2
Use it with object
use Illuminate\Support\Fluent;
$data = (object) [
'a' => 1,
'b' => 2,
];
dd($data['a']); // Cannot use object of type stdClass as array
$fluent = new Fluent($data);
dd($fluent['a']); // 1
$fluent->c = 3;
dd($fluent->toArray()); // array:3 [▼ "a" => 1 "b" => 2 "c" => 3]
unset($fluent['c']);
dd($fluent->toArray()); // array:2 [▼ "a" => 1 "b" => 2 ]
use more
$data = (object) [
'a' => 1,
'b' => 2,
];
$fluent = new Fluent($data);
$fluent->name('Morcos')->email('marcosgaad@gmail.com')->age(29)->isAdmin();
dd($fluent->toArray()); // array:6 [▼ "a" => 1 "b" => 2 "name" => "Morcos" "email" => "marcosgaad@gmail.com" "age" => 29 "isAdmin" => true ]
I hope you enjoyed the code added to you after new and useful information.
Top comments (0)