Skip to content

Commit e98de07

Browse files
committed
Api for posts as articles added
1 parent 949c600 commit e98de07

File tree

8 files changed

+65930
-11
lines changed

8 files changed

+65930
-11
lines changed

app/Http/Controllers/PostsController.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\User;
1212
// 10: For Using sql queries
1313
use DB;
14+
use App\Http\Resources\PostsResource;
1415

1516
class PostsController extends Controller
1617
{
@@ -21,7 +22,7 @@ class PostsController extends Controller
2122
*/
2223
public function __construct()
2324
{
24-
$this->middleware('auth', ['except' => ['index', 'show']]);
25+
$this->middleware('auth', ['except' => ['index', 'show', 'fetchall']]);
2526
}
2627

2728
/**
@@ -41,10 +42,29 @@ public function index()
4142
//$posts = Post::orderBy('created_at', 'desc')->take(2)->get();
4243
// 13.1.4: For Pagination:
4344
$posts = Post::orderBy('created_at', 'desc')->paginate(5);
44-
// 10.1.2: return posts.index, @ 13.2: inject $posts
45+
// 10.1.2: return posts.index, @ 13.2: inject $posts:
4546
return view('posts.index')->with('posts', $posts);
46-
47-
47+
}
48+
49+
/**
50+
* 10.1: Display a listing of the resource.
51+
*
52+
* @return PostsResource
53+
*/
54+
public function fetchall()
55+
{
56+
// 10.1.1: Using sql query to fetch all Posts:
57+
//$posts = DB::select('SELECT * FROM posts');
58+
// 13.1.1: Fetch all data using eleqouent:
59+
//$posts = Post::all();
60+
// 13.1.2: Order by Desc:
61+
//$posts = Post::orderBy('created_at', 'desc')->get();
62+
// 13.1.3: For Pulling limited records:
63+
//$posts = Post::orderBy('created_at', 'desc')->take(2)->get();
64+
// 13.1.4: For Pagination:
65+
$posts = Post::orderBy('created_at', 'desc')->paginate(5);
66+
// 10.1.2: return posts.index, @ 13.2: inject $posts:
67+
return PostsResource::collection($posts);
4868
}
4969

5070
/**
@@ -127,6 +147,21 @@ public function show($id)
127147
// return Post::where('title', 'Post Two')->get();
128148
}
129149

150+
/**
151+
* 10.4: Display the specified resource.
152+
*
153+
* @param int $id
154+
* @return PostsResource
155+
*/
156+
public function fetchone($id)
157+
{
158+
// 10.4.1: Find the post with id sent & return the entire post using eleqouent
159+
$post = Post::findOrFail($id);
160+
return new PostsResource($post);
161+
// 10.4.2: To use where clause
162+
// return Post::where('title', 'Post Two')->get();
163+
}
164+
130165
/**
131166
* 10.5: Show the form for editing the specified resource.
132167
*
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use Illuminate\Http\Resources\Json\JsonResource;
6+
7+
class PostsResource extends JsonResource
8+
{
9+
/**
10+
* Transform the resource into an array.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @return array
14+
*/
15+
public function toArray($request)
16+
{
17+
return parent::toArray($request);
18+
}
19+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

public/css/app.css

Lines changed: 16423 additions & 6 deletions
Large diffs are not rendered by default.

public/js/app.js

Lines changed: 49446 additions & 1 deletion
Large diffs are not rendered by default.

routes/api.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
Route::middleware('auth:api')->get('/user', function (Request $request) {
1717
return $request->user();
1818
});
19+
20+
Route::get('articles', 'PostsController@fetchall');
21+
Route::get('article/{id}', 'PostsController@fetchone');

0 commit comments

Comments
 (0)