Skip to content

Commit 06430ae

Browse files
committed
added resources
1 parent ccf8227 commit 06430ae

File tree

7 files changed

+70
-5
lines changed

7 files changed

+70
-5
lines changed

app/Http/Controllers/PostController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Post;
66
use Illuminate\Http\Request;
77
use Symfony\Component\HttpFoundation\Response;
8+
use App\Http\Resources\Post as PostResource;
89

910
class PostController extends Controller
1011
{
@@ -15,7 +16,8 @@ class PostController extends Controller
1516
*/
1617
public function index()
1718
{
18-
return Post::latest()->get();
19+
$post = Post::latest()->get();
20+
return PostResource::collection($post);
1921
}
2022

2123
/**
@@ -39,7 +41,7 @@ public function store(Request $request)
3941
*/
4042
public function show(Post $post)
4143
{
42-
return $post;
44+
return PostResource::make($post);
4345
}
4446

4547
/**
@@ -51,7 +53,7 @@ public function show(Post $post)
5153
*/
5254
public function update(Request $request, Post $post)
5355
{
54-
$post = $request->all();
56+
$request['slug'] = str_slug($request->title);
5557
$post->update($request->all());
5658
return response('updated','200');
5759
}

app/Http/Resources/Post.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use Illuminate\Http\Resources\Json\JsonResource;
6+
use App\Http\Resources\User as UserResource;
7+
8+
class Post extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @return array
15+
*/
16+
public function toArray($request)
17+
{
18+
return [
19+
'id' => $this->id,
20+
'title' => $this->title,
21+
'slug' => $this->slug,
22+
'description' => $this->body,
23+
'category' => $this->category,
24+
'user' => new UserResource($this->user),
25+
'created_at' => $this->created_at->diffForHumans(),
26+
'updated_at' => $this->updated_at->diffForHumans(),
27+
];
28+
}
29+
}

app/Http/Resources/User.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use Illuminate\Http\Resources\Json\JsonResource;
6+
7+
class User 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 [
18+
'id' => $this->id,
19+
'name' => $this->name,
20+
'email' => $this->email,
21+
'created_at' => $this->created_at->diffForHumans(),
22+
'updated_at' => $this->updated_at->diffForHumans(),
23+
];
24+
}
25+
}

app/Post.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function comments(){
1919
return $this->hasMany(Comments::class);
2020
}
2121

22+
public function user(){
23+
return $this->belongsTo(User::class);
24+
}
25+
2226
public function cateogry(){
2327
return $this->belongsTo(Category::class);
2428
}

database/factories/CategoryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
$factory->define(Category::class, function (Faker $faker) {
99
return [
10-
'category'=> $faker->title,
10+
'category'=> $faker->word,
1111
'user_id'=> function(){
1212
return \App\User::all()->random();
1313
}

database/factories/PostFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'title' => $title,
1212
'slug' => str_slug($title),
1313
'body'=> $faker->text,
14-
'category'=> $faker->title,
14+
'category'=> $faker->word,
1515
'user_id'=> function(){
1616
return \App\User::all()->random();
1717
}

routes/api.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
* comments
99
*/
1010

11+
/* cors origin */
12+
header("Access-Control-Allow-Origin: *");
13+
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,Authorization ");
14+
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE");
15+
1116
Route::apiResource('posts','PostController');
1217
Route::apiResource('category','CategoryController');
1318
Route::apiResource('/post/{post}/comment','CommentsController');

0 commit comments

Comments
 (0)