如何在laravel中的视图中传递对象

时间:2016-07-25 16:28:50

标签: laravel eloquent laravel-5.2 blade laravel-blade

我正在foreach statement中的views中调用一组html代码,我正试图通过array of object推送controller来获取视图}。

以下是我的控制器代码:

public function get_template($id)
{
    $template = Template::findOrFail($id);
    $getplugin = json_decode($template->templatedata);
    $plugins = Plugin::all();
    $userid = $template->user->id;
    return view('nitseditor.test', ['plugins' => $plugins, 'template'=> $template, 'getplugin' => $getplugin, 'userid' => $userid]);
}

在我的观看次数中,我这样称呼:

@foreach($getplugin as $renderer)

   @include('themes.' . $template->theme->name . '.Plugins.' . $plugins->find($renderer)->type . '.' . $plugins->find($renderer)->id, ['content' => $plugins->find($renderer)->userplugins()->whereUserId($userid)->first()->pivot->contents])

@endforeach
正在生成

观看次数 HTML代码

<div id="slideshow">
   <div class="revolution-slider">
     <ul>
        <!-- SLIDE  -->
        @foreach($content->slider as $sliders)
           <li data-transition="{{ $sliders->transition }}" data-slotamount="{{ $sliders->slotamount }}" data-masterspeed="{{ $sliders->masterspeed }}">
              <!-- MAIN IMAGE -->
              <img src="{{ URL::asset($sliders->url) }}" alt="">
           </li>
        @endforeach
     </ul>
  </div>
</div>

现在我收到错误

  

尝试获取非对象的属性(查看:C:\ wamp \ www \ NitsEditor \ resources \ views \ themes \ Miracle \ Plugins \ Slider \ 2.blade.php)(查看:C:\ wamp \ www \ NitsEditor \资源\视图\主题\奇迹\插件\滑块\ 2.blade.php)

diedump执行foreach loop下面的代码:

foreach ($getplugin as $item){

        $plugincontents[] = Plugin::findOrFail($item)->userplugins()->whereUserId($userid)->first()->pivot->contents;

}
dd($plugincontents);

我能够获得保存该特定视图信息的JSON output。如下所示:

array:2 [▼
  0 => "{"slider": [{"url": "img/home/bg.jpg", "slotamount": "7", "transition": "zoomin", "masterspeed": "1500"}, {"url": "img/home/bg2.jpg", "slotamount": "7", "transition": "zoomout", "masterspeed": "1500"}, {"url": "img/home/LOMEg.png", "slotamount": "7", "transition": "slidedown", "masterspeed": "1500"}]}"
  1 => "{"logo": {"logolink": "index.html", "logoimage": "img/home/nitseditorlogo.png"}, "pages": [{"pagelink": "index.html", "pagename": "Mysite"}, {"pagelink": "templates.html", "pagename": "Templates"}, {"pagelink": "aboutus.html", "pagename": "About Us"}, {"pagelink": "contactus.html", "pagename": "Contact Us"}]}"
]

请帮帮我。感谢。

2 个答案:

答案 0 :(得分:1)

而不是$getplugin = json_decode($template->templatedata);使用$getplugin = json_decode($template->templatedata,true);

答案 1 :(得分:0)

好吧,在进行交叉检查时,我发现在视图中的数据中需要json_decode。我试过并得到了结果,

在我的观看次数中,我对数组json_decode做了如下:

@foreach($getplugin as $renderer)

    @include('themes.' . $template->theme->name . '.Plugins.' . $plugins->find($renderer)->type . '.' . $plugins->find($renderer)->id, ['contents' => json_decode($plugins->find($renderer)->userplugins()->whereUserId($userid)->first()->pivot->contents)])

@endforeach

最后根据需要得到结果。