【问题标题】:Declare an array variable to the controller/component [Laravel, October CMS]向控制器/组件声明一个数组变量 [Laravel, October CMS]
【发布时间】: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


    【解决方案1】:

    您正在尝试使用额外的$ 将数组指定为属性名称。拿出来就没事了。

    $this->results = $results;
    

    【讨论】:

    • 我花了很多时间试图解决这个问题。非常感谢您的帮助。我会尽快接受你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 2012-10-19
    相关资源
    最近更新 更多