Skip to content
4 changes: 3 additions & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;


class RegisterController extends Controller
{
Expand Down Expand Up @@ -67,7 +69,7 @@ protected function create(array $data)
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'api_token' => Str::random(80),
'api_token' => Str::random(80)
]);
}
}
26 changes: 12 additions & 14 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use App\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use DB;
// use DB;
use Illuminate\Support\Facades\DB;

class PostController extends Controller
{
Expand All @@ -23,32 +24,30 @@ public function index()
// var_dump($query->sql);
// });

$data = Post::with(['user', ])->paginate(4);
return new PostCollection($data);
// return response()->json(new PostCollection($data), 200);
$data = Post::with(['user'])->paginate(5); // eager load
return new PostCollection(($data));
// return response()->json($data, 200);
}

public function show($id)
public function show(Post $post)
{
$data = Post::find($id);

if (is_null($data)) {
// $data = Post::find($id);
if (is_null($post)) {
return response()->json([
'message' => 'Resource not found!'
'message' => 'Post not found'
], 404);
}

// return $data;

return response()->json(new PostResource($data), 200);
return new PostResource($post);
// return response()->json($post, 200);
}

public function store(Request $request)
{
$data = $request->all();

$validator = Validator::make($data, [
'title' => ['required', 'min:5']
'title' => ['required', 'min:5'],
]);

if ($validator->fails()) {
Expand All @@ -59,7 +58,6 @@ public function store(Request $request)

// $response = Post::create($data);
$response = request()->user()->posts()->create($data);

return response()->json($response, 201);
}

Expand Down
2 changes: 0 additions & 2 deletions app/Http/Resources/Post/PostCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class PostCollection extends ResourceCollection
public function toArray($request)
{
return [
// 'data' => $this->collection, //Not formatted

'data' => PostResource::collection($this->collection),
'meta' => [
'total_post' => $this->collection->count()
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Resources/Post/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public function toArray($request)
'title' => $this->title,
'body' => $this->body,
'stored_at' => $this->created_at->diffForHumans(),
// 'user' => $this->user
'user' => new UserResource($this->user),
'comments' => $this->comments
'user' => new UserResource($this->user)
];
}
}
12 changes: 6 additions & 6 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
class Post extends Model
{
protected $guarded = [];
protected $hidden = ['created_at', 'updated_at'];
protected $appends = ['stored_at'];
// protected $hidden = ['created_at', 'updated_at']; // hidden column
// protected $appends = ['stored_at']; // adding column

public function getStoredAtAttribute()
{
return $this->created_at->diffForHumans();
}
// public function getStoredAtAttribute()
// {
// return $this->created_at->diffForHumans();
// }

public function user()
{
Expand Down
Loading