【发布时间】:2016-11-02 21:27:58
【问题描述】:
我正在使用十月 CMS,但不知道如何将数组添加到与 Laravel 控制器类似目的的组件中。基本上它是为了我的网站搜索功能。问题是,它总是抛出一个数组到字符串的转换错误。如果你看代码,问题出在最后一行$this->$results = $results;,因为$results是一个多维数组。
/**
* Component setup.
*
* @return void
*/
public function onRun()
{
$this->search();
}
/**
* Initiate the search
*
*@return void
*/
public function search()
{
// Sets the parameters from the get request to the variables.
$popularno = Request::get('q');
// Perform the query using Query Builder
$estates = DB::table('makler_realestate_objects')
->where('slug', 'like', "%${popularno}%")
->get();
// Now build a results array
$i = 0;
foreach ($estates as $estate) {
$i++;
$results[$i] = [
'title' => $estate->prim_id,
'text' => $estate->sifra_id,
'url' => 'nepremicnina/' . $estate->slug,
];
}
print_r($results);
$this->$results = $results; // This is the issue
}
我想将它声明给组件,以便我可以在页面上使用{% set results = Search.results %} 调用它,并使用 for 循环遍历每个数组项。
{% for result in results %}
<li>{{result.title}} {{result.text}}</li>
{% endfor %}
感谢您的帮助。
【问题讨论】:
标签: php arrays symfony laravel octobercms