Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tayyabshahzad1's avatar

Relationship

Hi, I have tables of franchise products product_special_prices All products are stored in a product table with their price and details. But things get tricky when I set the product's special price. For setting special prices I am using the product_special_price table in that table I am storing product_id , franchse_id, price The reason why I am doing this is all products are listed in every franchise but if the admin wants to set a special price of the product for any franchise then this solution will work, now the problem is app developer wants me to send all products with their detail and price if the product has special price than it should show a special price else normal price will show in API wile time of fetching data. How can I do this in my controller? Currently, I am sending products like below

$products = Product::where('status','!=','inactive')->get();
        return response([
            'success' => true,
            'product' =>  ProductResource::collection($products),
        ]);
0 likes
6 replies
sr57's avatar

@tayyabshahzad1

Have you defined your relations? Please share them.

Your are not clear with franchise

admin wants to set a special price of the product for any franchise

no need to have franchise_id in the table product_special_prices if 'any' means all franchise

if not, when you send data, prise should depend of of the franchise too.

1 like
tayyabshahzad1's avatar

@sr57 Thanks for the reply, yes I defined but the relationship is with product and product_special_price and that is Product->hasMnay(SpecialPrice::class) and your second question is about no need to have franchise_id why did I set it up because. In my case, one table is the franchise and the second is a product, when I create any product its means when a user opens any franchise he will be able to see all products. For example: I have products A with a price of 10 Rs and B with a price of 20 Rs and the franchises are KFC & McDonald. now if the user opens KFC or McDonald it will see product A with 10 Rs & B with 20 Rs. Now system admin wants to set a special price for product B for KFC the default price is 20 Rs, but he wants when someone opens KFC the product B price will show 30 Rs instead of 20. This is my whole scenario hope you understand

sr57's avatar

@tayyabshahzad1

Original post : All products are stored in a product table with their price

Second post : I have products A with a price of 10 Rs and B with a price of 20 Rs and the franchises are KFC & McDonald

If prices depend of product and franchise you must have a table prices linked to tables products & franchises.

And when this works, you change it to add special prices.

tayyabshahzad1's avatar

@sr57 I have tried one more method and this almost works for me please guide me how can i get out form this I want to pass an additional parameter to the resource,

I have passed franchise_id to my resource collection now how can I get it in my resource array?

 $id = 1;
  return response([
            'success' => true,
            'product' =>  ProductResource::collection($products)->additional(['franchise_id'=>$id]),
        ]);
 return [
             'franchise_id' => $this->extra->franchise_id,
            'id' => $this->id,
            'name' => $this->name,
   ]
achatzi's avatar

@tayyabshahzad1 You can define a many-to-many relation for the Product and Franchise Models like this

//in the Franchise model
public function products()
{
	return $this->belongsToMany(Product::class, 'product_special_price')
		->withPivot([
			'price'
		]);
}

Now in the api when you want the prices for a franchise

$franchise = Franchise::with('products')->find($franchise_id);
Product::where('status','!=','inactive')->get();

$data = [];
foreach ($products as $product) {
	//check if the franchise has a special price or else get the product price
	$price = $franshise->products->where('id', $product->id)?->pivot?->price ?? $product->price;

	$data[] = [
		'id' => $product->id,
		'title' => $product->title,
		'price' => $price
	];
}

return response()->json([
	'success' => true,
    'product' =>  $data,
]);
sr57's avatar

@achatzi

I have tried one more method

No need to try if your models/relations don't fit your needs.

@achatzi propose you a solution , if it does not fit your need explain why.

Please or to participate in this conversation.