Categories

Thursday, January 9, 2025
#919814419350 therichposts@gmail.com
AngularAngular 19Laravel

Integrating Angular with a Laravel backend for an e-commerce project

Integrating Angular 19 with a Laravel backend for an e-commerce project

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

  1. Routes (routes/api.php):
use App\Http\Controllers\ProductController;

Route::get('/products', [ProductController::class, 'index']);
Route::post('/products', [ProductController::class, 'store']);
  1. 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);
    }
}
  1. 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

  1. 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);
  }
}
  1. 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;
    });
  }
}
  1. 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>
  1. 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

  1. Start Laravel:
php artisan serve
  1. 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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.