Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

I think you're doing it wrong. The values you store on the db has to be of a single currency. Lets say usd. You should always use that currency to query. So before you query a value in euro you will convert euro to usd then query and turn the results back to Euro. I think that's how most shopping carts does it.

0

I wouldn't do that in your query. I'd create a Money value object that would be charge of validating, converting, calculating tax, etc.. You can use the object anywhere and keeps the business logic out of the query and database is always just dealing with integers which get converted to currency via the money object.

0

astroanu said:

I think you're doing it wrong. The values you store on the db has to be of a single currency. Lets say usd. You should always use that currency to query. So before you query a value in euro you will convert euro to usd then query and turn the results back to Euro. I think that's how most shopping carts does it.

I cannot do that because the currency rates always change. So If the user entered 100 USD, And then later when converting from EUR for example it could be a totally different number.

KevinCreel said:

I wouldn't do that in your query. I'd create a Money value object that would be charge of validating, converting, calculating tax, etc.. You can use the object anywhere and keeps the business logic out of the query and database is always just dealing with integers which get converted to currency via the money object.

Im really a noob at this, so any explanations would be nice. :) And would that help me achieve what i want and still saving the currency that the user entered.

Btw, I have an option if all currency will be selected I will have to show every, so converting and saving to db for example only EUR would not work.

0

Would this query do it?


foreach($this->currencies as $key => $currency) {
	$rate = $this->converter->convert(Auth::user()->currency, $currency);
	if($key == 0) {
			if($data['price_min']) {
				$property = $property->Where(function ($q) use ($currency, $data, $rate) {
								$q->where('price_unit', $currency)->where('price', '>', ($data['price_min'] * $rate));
						});
			}
			if($data['price_max']) {
				$property = $property->Where(function ($q) use ($currency, $data, $rate) {
								$q->where('price_unit', $currency)->where('price', '<', ($data['price_max'] * $rate));
						});
			}
	} else {
			if($data['price_min']) {
				$property = $property->orWhere(function ($q) use ($currency, $data, $rate) {
								$q->where('price_unit', $currency)->where('price', '>', ($data['price_min'] * $rate));
				});
	}
			if($data['price_max']) {
				$property = $property->orWhere(function ($q) use ($currency, $data, $rate) {
								$q->where('price_unit', $currency)->where('price', '<', ($data['price_max'] * $rate));
			});
		}
	}
}
Last updated 8 years ago.
0

hey, to get a more closer idea can you describe what your app does ? is it like a shopping cart or just a money exchange or something ?

0

astroanu said:

hey, to get a more closer idea can you describe what your app does ? is it like a shopping cart or just a money exchange or something ?

Hello, thanks for trying to help. I figured it out. I use this code for quering.

if($data['price_min'] || $data['price_max']) {
			if(Auth::check()) {
				if(Auth::user()->currency != '') {
					if($data['price_min']) {
						$property = $property->where(function ($q) use ($data) {
							foreach($this->currencies as $key => $currency) {
								$rate = $this->converter->convert(Auth::user()->currency, $currency);
								if($key == 'AF') {
									$q->where(function ($qu) use ($data, $currency, $rate) {
										$qu->where('price_unit', $currency)->where('price', '>=', ($data['price_min'] * $rate));
									});
								} else {
									$q->orWhere(function ($qu) use ($data, $currency, $rate) {
										$qu->where('price_unit', $currency)->where('price', '>=', ($data['price_min'] * $rate));
									});
								}
							}
						});
					}
					if($data['price_max']) {
						$property = $property->where(function ($q) use ($data) {
							foreach($this->currencies as $key => $currency) {
								$rate = $this->converter->convert(Auth::user()->currency, $currency);
								if($key == 'AF') {
									$q->where(function ($qu) use ($data, $currency, $rate) {
										$qu->where('price_unit', $currency)->where('price', '<=', ($data['price_max'] * $rate));
									});
								} else {
									$q->orWhere(function ($qu) use ($data, $currency, $rate) {
										$qu->where('price_unit', $currency)->where('price', '<=', ($data['price_max'] * $rate));
									});
								}
							}
						});
					}
				} else {
					if($data['price_min']) {
						$property = $property->where('price', '>=', $data['price_min']);
					}
					if($data['price_max']) {
						$property = $property->where('price', '<=', $data['price_max']);
					}
				}
			}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Pecka40 pecka40 Joined 15 May 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.