salmazz45's avatar

Add tax in my eCommerce app

i am creating eCommerce application with laravel when i creating order i dont know where i can take the value of tax put it in the setting on the website or ?? please help me for the best solution to make it it my schema Schema::create('orders', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('user_id'); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->unsignedBigInteger('address_id'); $table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade'); $table->unsignedBigInteger('coupon_id')->nullable(); $table->foreign('coupon_id')->references('id')->on('coupons')->onDelete('set null'); $table->enum('status', ['in_progress', 'accepted', 'on_delivery', 'delivered', 'canceled']); $table->decimal('total_product_count'); $table->decimal('discount')->default(0); $table->decimal('total'); $table->decimal('total_paid'); $table->timestamps(); });

0 likes
5 replies
bobbybouwmann's avatar

We really can't read this code... You can use the backticks ``` and put your code between them. This way it will be displayed in a better way.

martinbean's avatar

@salmazz45 Create a column that stores the amount of tax at the time the order was placed:

$table->decimal('tax')->default(0);

However, I’d question some of your column choices. Do you really need to store “total product count” as a decimal? Is an order ever going to contain a partial product, i.e. 2.5 products?

Also, it’s pretty common practice to store monetary values as an integer in the lowest denominator rather than decimal values or floats. So if you’re charging in USD, you would store the cents value, i.e. if the order total was $12.45, you would store 1245 in your database. This helps stop cents going “missing” and getting weird results if doing a lot of arithmetic. For this reason, it’s also a good idea to store the currency code against values as well, so you could have a currency column in your table:

$table->string('currency', 3)->default('USD');
salmazz45's avatar

thank u for ur notes I will take it into consideration

Araw's avatar
Schema::create('orders', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('user_id'); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->unsignedBigInteger('address_id'); $table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade'); $table->unsignedBigInteger('coupon_id')->nullable(); $table->foreign('coupon_id')->references('id')->on('coupons')->onDelete('set null'); $table->enum('status', ['in_progress', 'accepted', 'on_delivery', 'delivered', 'canceled']); $table->decimal('total_product_count'); $table->decimal('discount')->default(0); $table->decimal('total'); $table->decimal('total_paid'); $table->timestamps(); });

Please or to participate in this conversation.