php compact()函数和laravel新手问题

时间:2019-03-02 18:39:19

标签: php laravel

对于laravel,当将集合传递给视图时。有时会在控制器中使用紧凑功能(如下所示):

 public function index()
 {
     $projects = Project::all();
     return view ('projects.index',compact('projects'));
 }

但是根据php手册,compact函数使用变量来创建数组。当Project::all()是一个数组而不是变量时,该函数为什么起作用?

在手册中给出了以下示例:

<?php
$city  = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";

$location_vars = array("city", "state");

$result = compact("event", "nothing_here", $location_vars);
print_r($result);
?>
  

结果为:Array(       [事件] => SIGGRAPH       [市] =>旧金山       [状态] => CA)

如果返回$ projects给出:

id: 1
title:  "test"
description:    "test2"
created_at: "2019-03-02 19:18:50"
updated_at: "2019-03-02 19:18:50"

这只是数据库的结果。已经是数组了。

我认为compact(project)应该按照手册给出错误。

0 个答案:

没有答案