0

I have Two models called Product and Store In Store model i have this codes:

public function products()
    {
        return $this->belongsToMany(Product::class,'product_store')->withPivot('price','qty','is_block');
    }

    public function scopeDistance($query, $latitude, $longitude, $distance, $columns = ['*'])
    {

        return $query->select($columns)
            ->selectRaw('( 6372.795477598 * acos( cos( radians(' . $latitude . ') ) *
            cos( radians(latitude) ) *
            cos( radians(longitude) - radians(' . $longitude . ') ) +
            sin( radians(' . $latitude . ') ) *
            sin( radians(latitude) ) ) )
            AS distance')
            ->having('distance', '<=', $distance);
    }

In Product model i have this code:


    public function store()
    {
        return $this->belongsToMany(Store::class, 'product_store', 'product_id', 'store_id')->withPivot('price', 'qty', 'is_block');
    }

 protected $with = ['brand', 'category', 'likes','store'];

And i have a resource for Product with this code:

 public function toArray($request)
    {
        $user = auth('api')->user();
        ($store_id = \App\Models\Store::distance($user->addresses()->first()->latitude,$user->addresses()->first()->longitude,10)->first());
        $store = \App\Models\Store::find($store_id);
        return [
            'id' => $this->id,
            'title' => $this->title,
            'description' => $this->description,
            'slug' => $this->slug,
            'price' => $this->price,
//            'real_price' => isset($store) ? $store->pivot->price : $this->pivot->price,
            'category_id' => $this->category_id,
            'viewed' => $this->viewed,
            'is_active' => $this->is_active,
            'qty_change' => $this->qty_change,
            'limit' => $this->limit,
            'images' => $this->images,
            'brand_id' => $this->brand_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'store' => $store ,
            'brand' => $this->brand,
            'category' => $this->category,
            'likes' => $this->likes,
            'liked' => auth('api')->check() ? ((DB::table('wish_lists')->where('product_id', $this->id)->where('user_id', $user->id)->first()) ? 1 : 0) : 0
        ];
    }

    public function with($request)
    {
        return [
            'meta' => [
                'store' => $this->store[0] ?? null,
                'category' => $this->category,
            ],
        ];
    }

So when i use $store = \App\Models\Store::find($store_id); it should return a pivot in my store relation but it is not doing this. Please help me to solve it

Thank you in advance

8
  • You've wrapped the arguments to withPivot in an array on one of your models. Commented Dec 8, 2020 at 22:16
  • BTW wrapping your code to prevent long lines helps you spot mistakes like this more easily. Commented Dec 8, 2020 at 22:17
  • @miken32 can you explain your meaning in one answer? i did not get it Commented Dec 8, 2020 at 22:24
  • I can't be any clearer. In Store::products() you've wrapped your arguments to withPivot() in an array. Commented Dec 8, 2020 at 22:25
  • 1
    dose my answer satisfy you? Commented Dec 23, 2020 at 21:51

1 Answer 1

1

if you want to load the pivot when you retrieve the store, you have to eager load it.

in Store.php Model:

protected $with = ['products'];

it seems this lines:

($store_id = \App\Models\Store::distance($user->addresses()->first()->latitude,$user->addresses()->first()->longitude,10)->first());

is returning Store Model and should be change :

$store = \App\Models\Store::distance($user->addresses()->first()->latitude,$user->addresses()->first()->longitude,10)->first();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.