0

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);
    }
Atlas-Pio
  • 1,063
  • 11
  • 26
  • `$this->id` is looking for a property on the class. Are you sure you don't mean `$this->product->id`? – Brian Thompson Jan 17 '20 at 17:23
  • @BrianThompson product is not array or object, it's just a number like `19` or `20`. it's id of the product which i'm trying to pass it to resource then with this id, bring whole last part of code(in last paragraph) to resource then do other queries. check my last code from last paragraph, which i'm using `->first()`, now i want to apply all those codes to every record i get from query. – Atlas-Pio Jan 17 '20 at 17:31
  • @BrianThompson when i return `$this->product` i'll get Number from `$request->product_id`, which it's just a number. – Atlas-Pio Jan 17 '20 at 17:33

0 Answers0