Declare an array variable to the controller/component [Laravel, October CMS]
Ad
I am using October CMS and have no idea how to add an array to the component which serves a similar purpose of the Laravel's controller. Basically it is for my site search functionality. The thing is, it always throws an Array to String conversion ERROR. If you look at the code, the problem lies at the last line $this->$results = $results;
because $results
is a multidimensional array.
/**
* 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
}
I want to declare it to the component so I can call it on the page with {% set results = Search.results %}
and loop through each array item with a for loop.
{% for result in results %}
<li>{{result.title}} {{result.text}}</li>
{% endfor %}
Thank you for your help.
Ad
Answer
Ad
You're trying to assign an array as a property name by using an extra $
. Take it out and you should be fine.
$this->results = $results;
Ad
source: stackoverflow.com
Related Questions
Ad
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad