Laravel控制器并添加到结果集中

时间:2017-11-03 14:53:32

标签: laravel-5

我已经检查了Q& A这个并且无法找到任何内容,所以我想问一下。

我有一个非常简单的Laravel控制器,通过'名称模型'从表中返回所有结果。然后还有一个对我的控制器的调用,通过模型计算行和所有工作并发送到结果集很好......

// All results from my 'Name' model:
$results = $this->name->getAllResults(); // All works fine.

// I then use my controller again, count the rows via the model and add them to $results:
$results['count'] = $this->countNames(); // Again fine

但是,当我尝试在$ results数组中添加一个字符串然后将其传递给视图时,如:

$results['test'] = 'Test'; // This fails in the view
$results['test'] = 124; // But this passes in the view and renders.

它似乎只允许我在我的结果集数组中添加一个INT。因为$results['test'] = 124也失败了。

我终于通过以下方式发送到我的视图:

return view('names', compact('results')); // And that works fine.

任何人都可以看到它是什么我错过了为什么整数添加到$ results工作而不是字符串?非常感谢提前。

1 个答案:

答案 0 :(得分:0)

您正在更新收集数据。以下行将给出模型集合。

$results = $this->name->getAllResults(); // This may give collection of the model

以下,您正在更新集合对象。

$results['count'] = $this->countNames();

您可以执行以下操作以安全地发送数据进行查看,而无需修改任何内容。

$results = $this->name->getAllResults();
$count = $this->countNames();
$test = 'Test';
$test2 = 124;
return view('names', compact('results','count','test','test2'));