Skip to content

Commit

Permalink
resources collection added
Browse files Browse the repository at this point in the history
  • Loading branch information
sarancruzer committed Dec 15, 2018
1 parent dc18bc4 commit 6f9f4ba
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@

- php artisan tinker

# Merge Json/Resource::with() when using a collection in an API Resource

- https://github.com/laravel/ideas/issues/781





Expand Down
12 changes: 10 additions & 2 deletions app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

use App\Product;

use App\Http\Resources\ProductResource;
use App\Http\Resources\ProductsResource;

class ProductController extends Controller
{

Expand Down Expand Up @@ -40,8 +43,13 @@ public function __construct(Request $request) {
*/
public function index()
{
$products = Product::with('user')->paginate(10);
return response()->json(['success' => '','data' => $products], 200);
// $products = Product::with('user')->paginate(10);

return $users = new ProductsResource(Product::with('user')->paginate(10));

// return new ProductsResource(Product::with(['user'])->paginate(10));

return response()->json(['success' => '','data' => $users], 200);

}

Expand Down
16 changes: 12 additions & 4 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ public function index()
//
// $users = User::paginate(10);
// $users = User::find(1)->product;
// $users = User::with('product')->paginate(10);
$users = UserResource::collection(User::with('product')->paginate(10));

return response()->json(['success' => '','result' => $users], 200);
$users = User::with('product')->paginate(10);
// $users = UserResource::collection(User::with('product')->paginate(10));
// $users = UserResource::make(User::with('product')->find(1));

// $users = UserResource::collection(User::with('product')->paginate(10));
// return $users;


// $resource = new UserResource($user);

$users = UserResource::make(User::with('product')->find(1));
return $users;

}

Expand Down
24 changes: 11 additions & 13 deletions app/Http/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@

use Illuminate\Http\Resources\Json\JsonResource;

class RatingResource extends JsonResource
class ProductResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/

public function toArray($request)
{
return [
'id' => $this->user_id,
'user_id' => $this->user_id,
'product_name' => $this->product_name,
'product_price' => $this->product_price,
'user' => $this->user,
];

print_r($request);
print_r($this);

return [
'id' => $this->user_id,
'user_id' => $this->user_id,
'product_name' => $this->product_name,
'product_price' => $this->product_price,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
'user' => $this->user,
];
}


}
23 changes: 13 additions & 10 deletions app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\Resource;

class UserResource extends JsonResource
class UserResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/

public function toArray($request)
{
return [
Expand All @@ -20,8 +15,16 @@ public function toArray($request)
'email' => $this->email,
'product' => $this->product,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
"first_page_url" => $this->first_page_url
'updated_at' => (string) $this->updated_at
];
}

public function with($request)
{
return [
'version' => '1.0',
'success' => true,
];
}

}

0 comments on commit 6f9f4ba

Please sign in to comment.