Hello i have a complicated issue which base on this Solution, I've managed to pass parameter to my API Resource, but before passing Parameter i could get my id with $this->id
, now I'm getting this error:
'Property [id] does not exist on this collection instance.'
Here's How i was sending my Query to API resource :
Controller :
return SellerRepresentativeResources::collection($representative);
API Resource :
public function toArray($request)
{
return [
'id' => $this->id
];
}
In above method i didn't need to pass parameter, now base on top solution these are the methods I've tried :
Controller :
$representative = Representatives::where('distributor_id', $request->distributor_id)->latest()->get();
// I've got `"Method Illuminate\\Support\\Collection::product does not exist."` when i used below method
return SellerRepresentativeResources::collection($representative)->product($request->product_id);
// in this method i could access my product_id with $this->product_id but still i can't get my $this-id
return SellerRepresentativeResources::make($representative)->product($request->product_id);
//in this method i could access my product_id with $this->product_id but still i can't get my $this-id
return (new SellerRepresentativeResources($representative))->product($request->product_id);
API Resource :
class SellerRepresentativeResources extends Resource
{
protected $product;
public function product($value){
$this->product = $value;
return $this;
}
// when i use $this->id i'm getting above error
public function toArray($request)
{
return [
'id' => $this->id
];
}
// when i use `parent...` i'm getting passed data from query not $prodcut.
public function toArray($request)
{
return parent::toArray($request);
}
}
The reason i'm trying to pass parameter is that the parameter i've tried to send is used for some other queries which i'm trying to make it work base on product_id & some other results. check below code which is working when i use ->first()
but as u know i'm getting one result with ->first()
.
$representative = Representatives::where([
['state_id', auth()->user()->detail->state_id],
['distributor_id', $request->distributor_id]
])->first();
if ($representative != null) {
$generated = !! $representative->generate->where(
'representative_id', $representative->id
)->where(
'marketer_id', auth()->id()
)->count();
if ($generated) {
$requestItem = $representative->generate->where('marketer_id',auth()->id())->first();
$requestItemArray = array(
'id' => $requestItem->id,
'marketer_id' => $requestItem->id,
'representative_id' => $requestItem->id,
'path' => $requestItem->path
);
$requestList = GeneretaedStateCentralRequestItemsResource::collection($requestItem->items);
$checkExistItem = $representative->generate->where(
'representative_id', $representative->id
)->where(
'marketer_id', auth()->id()
)->first();
$checkProductItem = !! MarketersRepresentativeRequestItems::where(
'representative_request_id', $checkExistItem->id
)->where(
'product_id', $request->product_id
)->count();
if ($checkProductItem) {
$getProdcutItem = MarketersRepresentativeRequestItems::where(
'representative_request_id', $checkExistItem->id
)->where(
'product_id', $request->product_id
)->first();
}
} else {
$checkProductItem = false;
$requestList = false;
$requestItem = $representative->generate->where('marketer_id',auth()->id())->first();
$checkRequestITem = !! $requestItem;
if ($checkRequestITem) {
$requestItemArray = array(
'id' => $requestItem->id,
'marketer_id' => $requestItem->id,
'representative_id' => $requestItem->id,
'path' => $requestItem->path
);
} else {
$requestItemArray = null;
}
}
if ($checkProductItem)
{
$representativeArray = array([
'id' => $representative->id,
'address' => $representative->address,
'phone' => $representative->phone,
'town_name' => $representative->town->name,
'generated' => $representative,
'checkProduct' => $checkProductItem,
'items' => $requestList,
'productDetail' => $getProdcutItem,
'request_item' => $requestItemArray,
'detailAlert' => false
]);
} else {
$representativeArray = array([
'id' => $representative->id,
'address' => $representative->address,
'phone' => $representative->phone,
'town_name' => $representative->town->name,
'generated' => $generated,
'items' => $requestList,
'checkProduct' => $checkProductItem,
'request_item' => $requestItemArray,
'detailAlert' => false
]);
}
return SellerRepresentativeResources::collection($representativeArray);
}
else {
return response(['message' => 'NoData'],Response::HTTP_ACCEPTED);
}