Laravel blade include file with content example; In this tutorial you will learn How to Include one blade to another blade file with content in Laravel. This tutorial will gives you simple example of include a sub-view in Blade templates in Laravel.
laravel provide @include directive to include sub view inside blade file. We will show how to include blade tempate in another file? Let’s see the below example to include child (included) file to parent blade file.
Main Default File:
Suppose we have an index default main file something like this
resources/views/products/index.blade.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Product List</h1>
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Details</th>
</tr>
@foreach ($products as $product)
@include('products.details', ['product' => $product])
@endforeach
</table>
</div>
</body>
</html>
Included File:
Here the included file which will show the table recrods;
resources/views/products/details.blade.php
<tr>
<td>{{ $product->id }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->detail }}</td>
</tr>
Controller File Code:
Here is the controller file where from we are sending the array content data.
app\Http\Controllers\ProductController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
public function index()
{
$protucts = Product::all();
return view('products.index', compact('protucts'));
}
}
Hope its work for you.