I have an interesting question about compact in PHP and compact in Laravel.
Take a compact example in PHP as an example:
$banana = "yellow"; $apple = "red"; $result = compact('banana','apple'); var_dump($result); //Output array(2) { ["banana"]=> string(6) "yellow" ["apple"]=> string(3) "red" }
But when I use compact return view on controller in Laravel, it returns variable instead of array
public function fruisColor($banana="yellow",$apple="red"){ return view('template.fruits',compact('banana','apple')); }
But when I get this variable in template blade, it is not an array but a variable, see:
P1 = {{ $banana }} e P1 = {{ $apple }}
If PHP compactly converts variables to arrays, why in template blade does it only return var? This should not be:
P1 = {{ $banana[0] }} e P1 = {{ $apple[0] }}
Looks confusing, doesn’t it?
Since the two arguments to the view method accept an array, which according to the documentation converts them into variables that can be used in blade templates, Laravel can handle arrays returned from compact.