Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
laravel
laravel view
communitycreator

How to render data to the view from a controller using Laravel

Chinweuba Elijah Azubuike
widget

In this shot, we will learn how to render data parsed to the view from a controller, assuming we are going to be rendering the data from this controller.

Note: An understanding of controllers is assumed of the reader.

public function create()
    { 	

$posts= Post::all(); 
         return view('add_course',compact('posts'));
      
            
    }

Using the compact() method, we parse it as a string. Now in the view, we have to iterate through values of the $post variable like so:

Note: Please reference this Link to view my previous shot on using the compact() method.

<div  class="form-group mb-24pt">
     @foreach($posts as $post)
{{<p> $post->content </p>}}                           
       @endforeach                          
</div>

{{}} is a blade syntax that echos PHP content. The @foreach() is also a blade syntax Laravel implements for iteration. Blade has a lot of benefits that work easily with your code.

RELATED TAGS

php
laravel
laravel view
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring