声明一个数组变量到 controller/component [Laravel, October CMS]
Declare an array variable to the controller/component [Laravel, October CMS]
我正在使用 October CMS,但不知道如何将数组添加到与 Laravel 的控制器具有类似用途的组件中。基本上它是为了我的网站搜索功能。问题是,它总是抛出 Array to String conversion ERROR。如果您查看代码,问题出在最后一行 $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 %}
感谢您的帮助。
您正在尝试使用额外的 $
将数组指定为 属性 名称。拿出来应该就没事了
$this->results = $results;
我正在使用 October CMS,但不知道如何将数组添加到与 Laravel 的控制器具有类似用途的组件中。基本上它是为了我的网站搜索功能。问题是,它总是抛出 Array to String conversion ERROR。如果您查看代码,问题出在最后一行 $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 %}
感谢您的帮助。
您正在尝试使用额外的 $
将数组指定为 属性 名称。拿出来应该就没事了
$this->results = $results;