Pass Objects List from Controller to View in Laravel

  1. Download and install the latest version of Composer from https://getcomposer.org/download/

  2. Open Windows Terminal in Visual Studio Code install Laravel Installer with command as below:

    composer global require laravel/installer
  3. Create new folder named LearnLaravelWithRealApps. Use Visual Studio Code open to LearnLaravelWithRealApps foler. In this folder, create new project named LearnLaravelWithRealApps with command as below:

    laravel new LearnLaravelWithRealApps

  4. Run LearnLaravelWithRealApps project with command as below:

    php artisan serve
  5. Open LearnLaravelWithRealApps project with url as below:

    http://localhost:8000

Create new folder named images in public folder. In this folder, put the images to display in the view

Create new PHP file named Product.php in app/Models folder as below:

<?php

namespace App\Models;

class Product
{
    var $id;
    var $name;
    var $price;
    var $quantity;
    var $photo;

    function __construct($id, $name, $price, $quantity, $photo)
    {
        $this->id = $id;
        $this->name = $name;
        $this->price = $price;
        $this->quantity = $quantity;
        $this->photo = $photo;
    }
}

Create new PHP file named DemoController.php in app/Http/Controllers folder as below:

<?php

namespace App\Http\Controllers;

use App\Models\Product;

class DemoController extends Controller
{
	public function index()
	{
		$data = array(
			'products' => array(
				new Product('p01', 'name 1', 2, 3, 'thumb1.gif'),
				new Product('p02', 'name 2', 7, 2, 'thumb1.gif'),
				new Product('p03', 'name 3', 5, 8, 'thumb3.gif')
			)
		);
		return view('demo/index')->with($data);
	}
}

Create new folder named demo in resources/views folder. In this folder, create new Blade file named index.blade.php as below:

<html>

<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Laravel</title>
</head>

<body>

	<h3>Product List</h3>
	<table border="1" cellpadding="2" cellspacing="2">
		<tr>
			<th>Id</th>
			<th>Name</th>
			<th>Photo</th>
			<th>Price</th>
			<th>Quantity</th>
			<th>Sub Total</th>
		</tr>
		@foreach($products as $product)
		<tr>
			<td>{{$product->id}}</td>
			<td>{{$product->name}}</td>
			<td><img src="{{asset('images/'.$product->photo)}}"></td>
			<td>{{$product->price}}</td>
			<td>{{$product->quantity}}</td>
			<td>{{$product->price * $product->quantity}}</td>
		</tr>
		@endforeach
	</table>

</body>

</html>

Open web.php file in routes folder, add new routes as below:

<?php
				
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\DemoController;

Route::get('/', [DemoController::class, 'index']);
Route::get('/demo', [DemoController::class, 'index']);
Route::get('/demo/index', [DemoController::class, 'index']);

Access index action in Demo controller with urls as below:

  • http://localhost:8000
  • http://localhost:8000/demo
  • http://localhost:8000/demo/index

Output