Hello guys how are you? Welcome back on my blog Therichpost. Today in this post I am going to share Integrating Angular with a Laravel backend for an e-commerce project.
Angular 19 came. If you are new then you must check below two links:
Now guys here is the complete code snippet and please follow carefully:
Sure! Here’s a basic code snippet for integrating Angular 19 with a Laravel backend for an e-commerce project. This example demonstrates creating a product listing page:
Laravel API – Backend
- Routes (routes/api.php):
use App\Http\Controllers\ProductController; Route::get('/products', [ProductController::class, 'index']); Route::post('/products', [ProductController::class, 'store']);
- Controller (app/Http/Controllers/ProductController.php):
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Product; class ProductController extends Controller { public function index() { return response()->json(Product::all(), 200); } public function store(Request $request) { $product = Product::create($request->all()); return response()->json($product, 201); } }
- Model (app/Models/Product.php):
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; protected $fillable = ['name', 'price', 'description']; }
Angular – Frontend
- Service (src/app/services/product.service.ts):
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class ProductService { private apiUrl = 'http://localhost:8000/api/products'; constructor(private http: HttpClient) {} getProducts(): Observable<any> { return this.http.get<any>(this.apiUrl); } addProduct(product: any): Observable<any> { return this.http.post<any>(this.apiUrl, product); } }
- Component (src/app/components/product-list/product-list.component.ts):
import { Component, OnInit } from '@angular/core'; import { ProductService } from '../../services/product.service'; @Component({ selector: 'app-product-list', templateUrl: './product-list.component.html', styleUrls: ['./product-list.component.css'] }) export class ProductListComponent implements OnInit { products: any[] = []; constructor(private productService: ProductService) {} ngOnInit(): void { this.productService.getProducts().subscribe((data) => { this.products = data; }); } }
- HTML Template (src/app/components/product-list/product-list.component.html):
<div> <h1>Product List</h1> <ul> <li *ngFor="let product of products"> <strong>{{ product.name }}</strong> - ${{ product.price }} <p>{{ product.description }}</p> </li> </ul> </div>
- App Module (src/app/app.module.ts): Ensure
HttpClientModule
is imported:
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { HttpClientModule } from '@angular/common/http'; import { AppComponent } from './app.component'; import { ProductListComponent } from './components/product-list/product-list.component'; @NgModule({ declarations: [ AppComponent, ProductListComponent ], imports: [ BrowserModule, HttpClientModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Run the Project
- Start Laravel:
php artisan serve
- Start Angular:
ng serve
Now, your Angular app should be able to fetch and display products from the Laravel API! Let me know if you need further refinements. ????
Ajay
Thanks
Recent Comments