<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stefan Caky</title>
    <description>The latest articles on DEV Community by Stefan Caky (@stefancoding).</description>
    <link>https://dev.to/stefancoding</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F634396%2Fdc85f7ff-ac88-41d7-9651-5011802b745e.jpeg</url>
      <title>DEV Community: Stefan Caky</title>
      <link>https://dev.to/stefancoding</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stefancoding"/>
    <language>en</language>
    <item>
      <title>How to use a loop in blade files - Laravel</title>
      <dc:creator>Stefan Caky</dc:creator>
      <pubDate>Fri, 17 Dec 2021 01:45:47 +0000</pubDate>
      <link>https://dev.to/stefancoding/how-to-use-loop-in-blade-files-laravel-323n</link>
      <guid>https://dev.to/stefancoding/how-to-use-loop-in-blade-files-laravel-323n</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When we talk about the loop in the blade, we mean PHP array passed into HTML template what called blade in Laravel. There is some way to loop in the blade. I will show you the easiest way, with the controller. &lt;/p&gt;

&lt;h2&gt;
  
  
  Let's go
&lt;/h2&gt;

&lt;p&gt;When we successfully installed the new Laravel application we will  create Controller called ProjectsController.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:controller ProjectsController -r
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we get all rows from the database under the Projects column. We use the Project model to get it. Save all projects into the &lt;code&gt;$projects&lt;/code&gt; variable then we pass it into the blade template.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use App\Models\Project;

public function index()
    {
        $projects = Project::all();

        return view('projects', ['projects' =&amp;gt; $projects]);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create blade file in &lt;em&gt;view&lt;/em&gt; folder called &lt;em&gt;project.blade.php&lt;/em&gt; and we use &lt;code&gt;@foreach&lt;/code&gt; to loop over the &lt;code&gt;$project&lt;/code&gt; variable what we got from &lt;em&gt;ProjectsController&lt;/em&gt; using table structure to format the results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;table class="table table-bordered" &amp;gt;
   &amp;lt;thead&amp;gt;
      &amp;lt;tr&amp;gt;
         &amp;lt;th&amp;gt;Project Title&amp;lt;/th&amp;gt;
         &amp;lt;th&amp;gt;Project description&amp;lt;/th&amp;gt;
         &amp;lt;th&amp;gt;Created&amp;lt;/th&amp;gt; 
      &amp;lt;/tr&amp;gt;
   &amp;lt;/thead&amp;gt;

   &amp;lt;tbody&amp;gt;
      @foreach($projects as $project)
         &amp;lt;tr&amp;gt;
            &amp;lt;th&amp;gt;{{ @project-&amp;gt;title }}&amp;lt;/th&amp;gt;
            &amp;lt;th&amp;gt;{{ @project-&amp;gt;description }}&amp;lt;/th&amp;gt;
            &amp;lt;th&amp;gt;{{ @project-&amp;gt;created_at }}&amp;lt;/th&amp;gt;
         &amp;lt;/tr&amp;gt;
      @endforeach
   &amp;lt;/tbody&amp;gt;
&amp;lt;/table&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;See how easy to use the loop method in Laravel? When we just pass some array from the controller into blade file.   &lt;/p&gt;

</description>
      <category>loop</category>
      <category>blade</category>
      <category>laravel</category>
      <category>loopinblade</category>
    </item>
  </channel>
</rss>
