-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_fixed.php
More file actions
46 lines (44 loc) · 1.57 KB
/
Copy pathapi_fixed.php
File metadata and controls
46 lines (44 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
Route::get('/books', function () {
try {
$books = DB::table('books')
->join('authors', 'books.author_id', '=', 'authors.author_id')
->leftJoin('images', 'books.book_id', '=', 'images.book_id')
->select(
'books.book_id as id',
'books.name',
'books.price',
'books.description',
'authors.name as author',
DB::raw('ANY_VALUE(images.image_url) as image')
)
->groupBy('books.book_id')
->get();
return response()->json($books);
} catch (\Throwable $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
});
Route::get('/books/{id}', function ($id) {
try {
$book = DB::table('books')
->join('authors', 'books.author_id', '=', 'authors.author_id')
->join('businesses', 'books.business_id', '=', 'businesses.business_id')
->leftJoin('images', 'books.book_id', '=', 'images.book_id')
->where('books.book_id', $id)
->select(
'books.book_id as id',
'books.*',
'authors.name as author',
'businesses.store_name as business_name',
'images.image_url as image'
)
->first();
return response()->json($book);
} catch (\Throwable $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
});