Create Database
Create new database named laravel_db. In this database, create new table named product as below:
Structure of Product Table
Data of Product Table
Install Laravel
-
Download and install the latest version of Composer from https://getcomposer.org/download/
-
Open Windows Terminal in Visual Studio Code install Laravel Installer with command as below:
composer global require laravel/installer
-
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
-
Run LearnLaravelWithRealApps project with command as below:
php artisan serve
-
Open LearnLaravelWithRealApps project with url as below:
http://localhost:8000
Connect to Database
Open .env file in root folder. Add values as below connect to database:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=
Clear Config Cache
Open Windows Terminal in Visual Studio Code and clear config cache with command as below:
php artisan config:clear
Create Product Model
Create new PHP file named Product.php in app/Models folder as below:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $primarykey = 'id';
protected $table = 'product';
public $timestamps = false;
}
Create ProductRepository Interface
Create new folder named Repositories. In Repositories folder, create new folder named Product. Create new PHP file named ProductRepository.php in app/Repositories/Product folder as below:
<?php
namespace App\Repositories\Product;
interface ProductRepository
{
public function findPriceLessThanOrEqual($price);
}
Create ProductRepositoryImpl Class
Create new PHP file named ProductRepositoryImpl.php in app/Repositories/Product folder as below:
<?php
namespace App\Repositories\Product;
use App\Models\Product;
class ProductRepositoryImpl implements ProductRepository
{
public function findPriceLessThanOrEqual($price)
{
return Product::where('price', '<=', $price)->get();
}
}
Declare Product Repository
Open AppServiceProvider.php file in app/Providers folder and add declare new repository to register method as below:
<?php
namespace App\Providers;
use App\Repositories\Product\ProductRepository;
use App\Repositories\Product\ProductRepositoryImpl;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(ProductRepository::class, ProductRepositoryImpl::class);
}
public function boot()
{
}
}
Controllers
Create new PHP file named DemoController.php in app/Http/Controllers folder as below:
<?php
namespace App\Http\Controllers;
use App\Repositories\Product\ProductRepository;
class DemoController extends Controller
{
protected $productRepository;
public function __construct(ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
}
public function index()
{
$data = array(
'products' => $this->productRepository->findPriceLessThanOrEqual(10)
);
return view('demo/index')->with($data);
}
}
Views
Create new folder named demo in resources/views folder. In this folder, create new Blade file named index.blade.php as below:
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h3>Index</h3>
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Status</th>
<th>Created</th>
<th>Description</th>
</tr>
@foreach($products as $product)
<tr>
<td>{{$product->id}}</td>
<td>{{$product->name}}</td>
<td>{{$product->price}}</td>
<td>{{$product->quantity}}</td>
<td>{{$product->status}}</td>
<td>{{$product->created}}</td>
<td>{{$product->description}}</td>
</tr>
@endforeach
</table>
</body>
</html>
Routes
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']);
Structure of Laravel Project
Run Application
Access index action in Demo controller with urls as below:
- http://localhost:8000
- http://localhost:8000/demo
- http://localhost:8000/demo/index
Output