2016-07-25 40 views
1

我在views中正在調用foreach statement中的一組html代碼,我試圖通過controller來推送array of object以獲取視圖。如何在laravel中傳遞視圖中的對象

以下是我控制器代碼:

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 \資源\意見\主題\奇蹟\插件\滑塊\ 2.blade.php)(查看:C:\瓦帕\ 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"}]}" 
] 

請幫幫我。謝謝。

回答

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 

並根據需要最後得到的結果。

1

代替$getplugin = json_decode($template->templatedata);使用$getplugin = json_decode($template->templatedata,true);

+0

仍然面臨同樣的錯誤。 –

+2

如果將'true'添加到'json_decode'中,則可能需要在foreach循環中訪問變量,如$ sliders ['transition']',因爲它不再是PHP對象。 –

+0

@DavidAngel:感謝您的指導。你們幫我找到答案。 –