Laravel compact()
P粉697408921
P粉697408921 2023-12-13 23:50:03
0
1
445

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?

P粉697408921
P粉697408921

reply all(1)
P粉502608799

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!