HTTP GET vs QUERY: Memahami Perbezaan Melalui Sistem Inventori Kedai Runcit dengan Laravel 13
Dalam pembangunan aplikasi web, kita biasanya menggunakan HTTP GET untuk mendapatkan data daripada server. Apabila pengguna mahu mencari produk, menapis stok atau melihat senarai inventori, GET sudah menjadi pilihan yang biasa. Tetapi bagaimana pula dengan HTTP QUERY ? Laravel 13 memperkenalkan sokongan routing untuk HTTP QUERY melalui Route::query() . Method ini menarik kerana ia membolehkan…
In the development of web applications, we typically use HTTP GET to retrieve data from the server. When users want to search for products, filter stock, or view an inventory list, GET is the usual choice. However, how does HTTP QUERY compare? Laravel 13 introduces routing support for HTTP QUERY via Route::query(). This method is advantageous because it allows us to send search criteria through the request body, rather than relying solely on the URL query string.
To understand their differences, we'll build a simple example using a mini retail store inventory system.
First, let's familiarize ourselves with GET and QUERY. Both are used to retrieve information from the server, but the way they send search criteria differs. HTTP GET usually sends parameters via the URL. For example: GET /products?category = food & stock = 10. In Laravel, we can read these parameters using: $request->query('category'); $request->query('stock'); The advantage of GET is that the URL can be saved as a bookmark, easily shared with other users, and suitable for searching, filtering, and pagination. It aligns with common browser and HTTP caching usage.
Now, let's consider QUERY. QUERY allows us to send search criteria through the request body. For example: QUERY / products / query Content-Type: application/json { "categories" : [ "Food" , "Oil" ] , "stock" : { "min" : 0 , "max" : 10 } } Importantly, QUERY is not a URL query string nor $request-query() in Laravel. It's a different HTTP method. Its advantage is that we can send more complex data structures without overloading them all into the URL.
Scenario: Mini retail store inventory system. Imagine we have an inventory system with product information like this: SKU Product Category Price Stock P001 Rice 5KG Food RM18.50 5 P002 Cooking Oil 1KG Oil RM3.50 8 P003 Sugar 1KG Food RM2.85 25 P004 Bath Soap Care Product RM4.50 3 P005 Wheat Flour Food RM3.20 12 We want to provide two functions: basic stock search using GET and advanced inventory search using QUERY.
3. Set up database and model. Using Laravel migrations, we create the products table. php artisan make:model Product -m Migration: Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('sku')->unique(); $table->string('name'); $table->string('category'); $table->decimal('price', 10, 2); $table->unsignedInteger('quantity')->default(0); $table->timestamps(); }) ; Run migration: php artisan migrate.
For this proof-of-concept, we use SQLite so we can easily test without needing an additional database server.
4. Using GET for basic stock search. Let's say a user wants to search products based on name or category. The URL sent: GET /products?search = rice. Register route in routes/web.php: use App\Http\Controllers\ProductController; Route::get('/products', [ProductController::class, 'index'])->name('products.index'); Controller: public function index(Request $request) { $products = Product::query() ->when($request->query('search'), fn ($query, $search) => $query->where('name', 'like', "%{$search}%")) ->when($request->query('category'), fn ($query, $category) => $query->where('category', $category)) ->paginate(10) ->withQueryString(); return view('products.index', compact('products')); } In Blade, we only need a simple HTML form: form method="GET" action="{{ route('products.index') }}"> <input type="text" name="search" placeholder="Search products..." value="{{ request('search') }}"> <button type="submit">Search</button> </form> When a user searches for products, the browser generates a URL like: /products?search=rice.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to