Skip to content
Advertisement

Laravel withPivot is not returning pivot extra fields

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 = AppModelsStore::distance($user->addresses()->first()->latitude,$user->addresses()->first()->longitude,10)->first());
        $store = AppModelsStore::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 = AppModelsStore::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

Advertisement

Answer

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 = AppModelsStore::distance($user->addresses()->first()->latitude,$user->addresses()->first()->longitude,10)->first());

is returning Store Model and should be change :

$store = AppModelsStore::distance($user->addresses()->first()->latitude,$user->addresses()->first()->longitude,10)->first();
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement