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

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>
Code in blade

{{}} 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.

Free Resources