Nitish Kumar
Nitish Kumar

Reputation: 6276

How to pass object in views in laravel

I'm having a set of html codes which are being called in a foreach statement in views, I'm trying to push array of object to get the views through controller.

Following is my controller code:

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]);
}

In my views I'm calling like this:

@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

Views or HTML code which are being generated:

<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>

Now I'm getting an error of

Trying to get property of non-object (View: C:\wamp\www\NitsEditor\resources\views\themes\Miracle\Plugins\Slider\2.blade.php) (View: C:\wamp\www\NitsEditor\resources\views\themes\Miracle\Plugins\Slider\2.blade.php)

While executing diedump the below code in foreach loop:

foreach ($getplugin as $item){

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

}
dd($plugincontents);

I'm able to get the JSON output which holds the information of that particular view. Like this below:

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"}]}"
]

Please help me out. Thanks.

Upvotes: 1

Views: 1303

Answers (2)

Nitish Kumar
Nitish Kumar

Reputation: 6276

Well, while doing cross checks I came to know that I need to json_decode in my data in views. I tried and got the result,

In my Views I did json_decode to the array like this:

@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

And finally getting the results as desired.

Upvotes: 0

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92427

instead of $getplugin = json_decode($template->templatedata); use $getplugin = json_decode($template->templatedata,true);

Upvotes: 1

Related Questions