Overview
To enable the AI Agent which can “create order”,”search inventory” & “add products to cart” on your Laravel application, we need to create a bridge between our AI engine and your Database. This integration consists of two distinct communication channels:
Server-to-Server (API)
Secured by API Tokens. Used by the Shombhob Bot to search your DB and create orders securely in the background.
Browser-to-Server (Web)
Public access. Used to manipulate the User’s Session (Cookies) to add items to the cart.
Security Note
The Search and Order endpoints Can be protected by `auth:sanctum` (or passport). This document is for standerd and widely functions and tarms please customize them according to your own code.
Define Routes
These routes act as the “Entry Points” for the AI. We separate them into api.php for secure data fetching and web.php for user session management.
Route::middleware(['auth:sanctum'])->group(function () {
/**
* Endpoint: GET /api/shombhob/products
* Purpose: Allows the AI to query your database for product details (ID, Price, Name).
* Why needed: The AI cannot read your DB directly; it needs this JSON API to "see" your inventory.
*/
Route::get('/shombhob/products', [ShombhobBotController::class, 'search']);
/**
* Endpoint: POST /api/shombhob/order
* Purpose: Allows the AI to generate a valid Order ID and Payment Link.
* Why needed: To allow "Buy Now" functionality directly inside the chat.
*/
Route::post('/shombhob/order', [ShombhobBotController::class, 'createOrder']);
});
/**
* Endpoint: GET /magic-cart/{id}/{qty}
* Purpose: A utility route that adds an item to the current user's session cart.
* Why web.php?: API routes are stateless (no cookies). To add items to a user's browser cart,
* we must use a Web route that has access to the Session facade.
*/
Route::get('/magic-cart/{id}/{qty?}', [ShombhobBotController::class, 'magicCart'])->name('shombhob.magic-cart');
Controller Logic
What is this file?
The Controller serves as the logic layer. It translates the standardized JSON requests coming from the AI bot into your specific application logic (Eloquent queries, Order creation events) and formats the response back into the JSON structure the AI expects.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product; // TODO: Replace with your actual Product Model
use App\Models\Order; // TODO: Replace with your actual Order Model
class ShombhobBotController extends Controller
{
/**
* TOOL 1: PRODUCT SEARCH
* ----------------------
* Purpose: Filters products by name and maps them to a standard JSON format.
* Why mapping is needed: Your DB columns might be 'title' or 'cost', but the AI
* is trained to look for 'name' and 'price'. We standardize it here.
*/
public function search(Request $request) {
$query = $request->input('search'); // The keyword sent by AI
// 1. Perform the Database Query
$products = Product::where('name', 'LIKE', "%{$query}%")
->where('active', true) // Best practice: only show active items
->take(5)
->get();
// 2. Map to Standard JSON output
return $products->map(function($item) {
return [
'id' => $item->id,
'name' => $item->name,
'price' => $item->price,
];
});
}
/**
* TOOL 2: CREATE ORDER
* --------------------
* Purpose: Creates a "Pending" order and returns a URL where the user can pay.
* Critical: The returned 'payment_url' is what the AI sends to the user in chat.
*/
public function createOrder(Request $request) {
// 1. Validate inputs (AI usually sends these correctly, but validation is safe)
$validated = $request->validate([
'email' => 'required|email',
'product_id' => 'required',
'quantity' => 'integer|min:1'
]);
// 2. Create the Order in your system
$order = Order::create([
'customer_email' => $validated['email'],
'product_id' => $validated['product_id'],
'quantity' => $validated['quantity'],
'status' => 'pending_payment' // Initial status
]);
// 3. Return the Payment Link
// This allows the user to click "Pay Now" in the chat and land on your checkout page.
return response()->json([
'order_id' => $order->id,
'payment_url' => url("/checkout/pay/{$order->id}")
]);
}
/**
* TOOL 3: MAGIC CART LINK
* -----------------------
* Purpose: Manipulate User Session.
* Logic: Finds product -> Adds to Session Cart -> Redirects to Cart Page.
*/
public function magicCart($id, $qty = 1) {
// TODO: Use your specific cart library (e.g., Darryldecode, Gloudemans, or native Session)
// Example: \Cart::add($id, 'Product Name', 10.00, $qty);
return redirect('/cart')->with('success', 'Item added by Shombhob AI Assistant!');
}
}
Critical Data Mapping
Ensure your system accepts and returns the data exactly as defined below.
| Function | Input (Received) | Output (Expected) |
|---|---|---|
| Search Products | ?search=keyword |
JSON Array:[{ "id": 1, "name": "...", "price": 10 }]
|
| Create Order |
JSON Body:{ "email": "...", "product_id": 1, "quantity": 1 }
|
JSON Object:{ "payment_url": "https://...", "order_id": 55 }
|
|
Magic Cart Link Browser Request |
URL Parameters:/magic-cart/{id}/{qty}
|
Action: 1. Add item to User Session 2. HTTP 302 Redirect to /cart
|
Shombhob Connect
Submit your credentials and confirm JSON structure.
© 2026 Shombhob AI Solutions