Skip to content

Commit 295f3dd

Browse files
committed
updated based on feedback
1 parent d8bb526 commit 295f3dd

File tree

1 file changed

+157
-8
lines changed

1 file changed

+157
-8
lines changed

tutorial/markdown/php/quickstart-php-laravel/php-laravel.md

Lines changed: 157 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,27 +236,178 @@ For CRUD operations, we will use the [Key-Value operations](https://docs.couchba
236236

237237
### POST Airline
238238

239-
Create a new airline document using the POST endpoint.
239+
Open the `AirlineController.php` file and navigate to the `store` method. The request data is validated and then passed to the model to save the airline document.
240+
241+
```php
242+
// store method in AirlineController.php
243+
public function store(Request $request, $id)
244+
{
245+
if ($errorResponse = $this->validateRequest($request)) {
246+
return $errorResponse;
247+
}
248+
try {
249+
$data = $request->only($this->allowedAttributes);
250+
$airline = new Airline($data);
251+
$airline->saveAirline($id);
252+
return response()->json(['message' => 'Airline created successfully'], 201);
253+
} catch (\Exception $e) {
254+
return response()->json(['message' => 'Internal Server Error'], 500);
255+
}
256+
}
257+
```
258+
259+
We call the `saveAirline` method in the Airline model, which uses the [`upsert`](https://docs.couchbase.com/php-sdk/current/howtos/kv-operations.html#upsert) method from the Couchbase SDK. The upsert method inserts a new document or updates an existing one.
260+
261+
```php
262+
// saveAirline method in Airline model
263+
public function saveAirline($id)
264+
{
265+
$data = $this->attributesToArray();
266+
unset($data['id']);
267+
$this->bucket->scope('inventory')->collection('airline')->upsert($id, $data);
268+
}
269+
```
240270

241271
### GET Airline
242272

243-
Retrieve a specific airline document by its ID using the GET endpoint.
273+
Navigate to the `show` method in the `AirlineController.php` file. This method retrieves a specific airline document using its ID.
274+
275+
```php
276+
// show method in AirlineController.php
277+
public function show($id)
278+
{
279+
try {
280+
$airline = Airline::findAirline($id);
281+
if (!$airline) {
282+
return response()->json(['message' => 'Airline not found'], 404);
283+
}
284+
return response()->json($airline);
285+
} catch (\Exception $e) {
286+
return response()->json(['message' => 'Internal Server Error'], 500);
287+
}
288+
}
289+
```
290+
291+
We call the `findAirline` method in the Airline model, which uses the [`get`](https://docs.couchbase.com/php-sdk/current/howtos/kv-operations.html#retrieving-documents) method from the Couchbase SDK for key-value retrieval.
292+
293+
```php
294+
// findAirline method in Airline model
295+
public static function findAirline($id)
296+
{
297+
$instance = new static;
298+
$document = $instance->bucket->scope('inventory')->collection('airline')->get($id);
299+
$data = $document->content();
300+
return new static($data);
301+
}
302+
```
244303

245304
### PUT Airline
246305

247-
Update an existing airline document or create a new one if it doesn't exist using the PUT endpoint.
306+
The PUT operation uses the same `store` method as POST, leveraging the `upsert` functionality to either update an existing document or create a new one if it doesn't exist.
307+
308+
```php
309+
// Same store method handles both POST and PUT
310+
$airline->saveAirline($id); // Uses upsert internally
311+
```
312+
313+
The `upsert` operation in Couchbase automatically handles both insert and update scenarios, making it perfect for PUT requests.
248314

249315
### DELETE Airline
250316

251-
Delete an airline document by its ID using the DELETE endpoint.
317+
Navigate to the `destroy` method in the `AirlineController.php` file. This method deletes an airline document by its ID.
318+
319+
```php
320+
// destroy method in AirlineController.php
321+
public function destroy($id)
322+
{
323+
try {
324+
$airline = Airline::findAirline($id);
325+
if (!$airline) {
326+
return response()->json(['message' => 'Airline not found'], 404);
327+
}
328+
Airline::destroyAirline($id);
329+
return response()->json(['message' => 'Airline deleted successfully'], 200);
330+
} catch (\Exception $e) {
331+
return response()->json(['message' => 'Internal Server Error'], 500);
332+
}
333+
}
334+
```
335+
336+
We call the `destroyAirline` method in the Airline model, which uses the [`remove`](https://docs.couchbase.com/php-sdk/current/howtos/kv-operations.html#removing-documents) method from the Couchbase SDK.
337+
338+
```php
339+
// destroyAirline method in Airline model
340+
public static function destroyAirline($id)
341+
{
342+
$instance = new static;
343+
$instance->bucket->scope('inventory')->collection('airline')->remove($id);
344+
}
345+
```
252346

253347
### List Airlines
254348

255-
Retrieve a list of airlines with optional filtering by country and pagination using query parameters.
349+
Navigate to the `index` method in the `AirlineController.php` file. This method retrieves a list of airlines with optional country filtering and pagination.
350+
351+
```php
352+
// index method in AirlineController.php
353+
public function index(Request $request)
354+
{
355+
try {
356+
$offset = $request->query('offset', 0);
357+
$limit = $request->query('limit', 10);
358+
$country = $request->query('country');
359+
$airlines = Airline::getAllAirlinesByCountry($country, $offset, $limit);
360+
return response()->json($airlines);
361+
} catch (\Exception $e) {
362+
return response()->json(['message' => 'Internal Server Error'], 500);
363+
}
364+
}
365+
```
366+
367+
We call the `getAllAirlinesByCountry` method in the Airline model, which uses SQL++ queries to retrieve filtered results.
368+
369+
```php
370+
// getAllAirlinesByCountry method in Airline model
371+
public static function getAllAirlinesByCountry($country, $offset = 0, $limit = 10)
372+
{
373+
$query = "SELECT * FROM `travel-sample`.`inventory`.`airline`";
374+
if ($country) {
375+
$query .= " WHERE country = '$country'";
376+
}
377+
$query .= " LIMIT $limit OFFSET $offset";
378+
379+
$result = $instance->bucket->scope('inventory')->query($query);
380+
return collect($result->rows());
381+
}
382+
```
256383

257384
### Airlines to Destination
258385

259-
Find airlines that fly to a specific destination airport using SQL++ queries to join collections.
386+
This endpoint demonstrates SQL++ queries with JOINs to find airlines that fly to a specific destination airport.
387+
388+
```php
389+
// getAirlinesToAirport method in Airline model
390+
public static function getAirlinesToAirport($destinationAirportCode, $offset = 0, $limit = 10)
391+
{
392+
$query = "
393+
SELECT air.callsign, air.country, air.iata, air.icao, META(air).id AS id, air.name
394+
FROM (
395+
SELECT DISTINCT route.airlineid AS airlineId
396+
FROM `travel-sample`.`inventory`.`route` AS route
397+
WHERE route.destinationairport = '$destinationAirportCode'
398+
) AS subquery
399+
JOIN `travel-sample`.`inventory`.`airline` AS air ON META(air).id = subquery.airlineId
400+
LIMIT $limit OFFSET $offset";
401+
402+
$result = $instance->bucket->scope('inventory')->query($query);
403+
return collect($result->rows());
404+
}
405+
```
406+
407+
This query demonstrates advanced SQL++ features:
408+
- **Subquery**: Finds distinct airline IDs from routes to the destination
409+
- **JOIN**: Connects route and airline collections
410+
- **META()**: Accesses document metadata to get the document ID
260411

261412
## Running The Tests
262413

@@ -266,8 +417,6 @@ This command will execute all the test cases in your project.
266417
php artisan test
267418
```
268419

269-
```
270-
271420
## Project Setup Notes
272421

273422
This project was based on the standard [Laravel project](https://laravel.com/docs).

0 commit comments

Comments
 (0)