Ad

Declare An Array Variable To The Controller/component [Laravel, October CMS]

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

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
Ad