I want to get data that Price is greater than or equal to a specified value (taken from the input).
$minPrice = $request->min_price;
$maxPrice = $request->max_price;
$value = Dproduct::where(['status' => 1])->where('price', '>', $minPrice)->get();
But the Price is JSON in database, I can't get it. Currencies such as: VND, USD, TWD,...
All answers are respected!
Thanks so much!
if you are using laravel 5.7 or greater then you may be able to select within the JSON. See the docs here for more details.
Note: for this to work you must use json data type for your field in database.
$minPrice = $request->min_price;
$maxPrice = $request->max_price;
$value = Dproduct::where(['status' => 1])->where('price->USD', $minPrice)->get();
Related
This question already has answers here:
Natural ORDER in Laravel Eloquent ORM
(5 answers)
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 6 months ago.
MySQL Database table
Rates:
Id
Name
1
$13.00
2
$20.00
3
$13.75
4
$15.00
5
$100.00
I'm trying to sort the data ASC to show on the dropdown, based on the data you can clearly understand that we can't sort the data by Id.
Here is the same code
$sorted = $rates;
foreach ($rates as $key => $data){
if(preg_match('/[a-z]/', $data['name'])){
unset($sorted[$key]);
$sorted[] = $data;
}
}
Result:
Dropdown
Expected Result:
$13.00
$13.75
$15.00
$100.00
Can you help me to figure this out?
You can try this:
$collection = collect([
['price' => '$13.00'],
['price' => '$12.00'],
['price' => '$15.00'],
]);
$sorted = $collection->sortBy('price');
dd($sorted);
it's very simple instead of sorting data in php, sort data when query table
first of all delete $ in name column and save data as float and change column data type to float
now you can feel sql power
if you want to run sql query manually use this query below
select * from Rates order by Name asc
if you want to use laravel eloquent use this code below
Rate::orderBy('Name','asc')->get();
for more information about order in mysql query see this or in laravel eloquent see this
Guessing ur using eloquent u can sort data when getting it from databse with orderBy
$query = Model::class('params')->orderBy('name', 'asc')->get();
Then when u do #foreach for printing it would be ID for the one with lowest price up until highest, and u dont have to deal with collections.
There is no need to delete your $ sign and change your data structure.
i want to get maximum of price from table laptop in yii2.
how can i do that?
i used below code
<?
Laptop::find()->max('price')
?>
but it seems not working :(
how can i access this data.
and in other type i coded before like below
<?
$min=Laptop::find()->select('min(price)');
?>
but it s not working again ..
how to add this query ??
Tecnically you don't need an Active Recordefor get the max or min result you could try using active Query
$max = (new \yii\db\Query())
->from('laptop_table')
->max('price');
echo $max;
If you have the error
Missing argument 1 for yii\db\Query::max()
is because you must pass the column name for calculate the max
anyway the code above is equivalent to
$max = Laptop::find()->max('price');
Like below :
echo $max_price = Laptop::find()->max("price");
Multi select and max price in the product table with model product:
$product = \common\models\Product::find()->where(['status'=> 1])->andWhere(['>','count',0])->max('price');
I am using laravel framework but in this WhereBetween not working. i am using price range where price starts form 1 to 500. when i set price 1-100
it gives me all the records that is in beetween 1 to 100 i.e. 20,40,50 etc. When i change the value from 1- 150 the above result will not be displayed it gives me no result(20,40,50). Can anyone help me . Here is my Code
enter code here
$products = Product::with("productimages")
->whereBetween('price',[$data['from'], $data['to']])
->get();
Note:- $data['from'] start value i.e 1 and $data['to'] end value i.e 150 or more
I just had the same issue. When the data type of the value is not an integer, it will behave very unpredictably. So the solution is to either change the datatype of the column or cast it while fetching, like this:
$from = $data['from'];
$to = $data['to'];
$products = Product::with("productimages")
->whereRaw("CAST(price AS UNSIGNED) BETWEEN ${from} AND ${to}")
->get();
I actually had this issue on the PostgreSQL jsonb data type, as it always returns nested values as strings, and between fetching doesn't work properly.
Using Where Between
$projects = Product::where('created_at', '>', $data['from'])
->where('created_at', '<', $data['to'])
->get();
OR
$current = Product::with("productimages")
->whereBetween('created_at', array($data['from'], $data['to']))->get();
OR
DB::table('Product')->whereBetween('created_at', array($data['from'], $data['to']))->get();
Hi I want to get order collection with multiple field values for same field.Already I tried to get the collections with addFieldToFilter for individual field values('status':pening & 'status':processing) and I merged both the collecions to a single collection.Now I'm getting problem like I can't able to access the resulted collection, its troughs error like getMethod can't call on non object.Give me any suggestions.
Here is my code :
$shippedCollection = array();
$processingCollection = array();
$orderSCollection = Mage::getModel('sales/order')->getCollection();
$shippedCollection = $orderSCollection->addFieldToFilter('status','delivered_carrier');
$orderPCollection = Mage::getModel('sales/order')->getCollection();
$processingCollection = $orderPCollection->addFieldToFilter('status','processing');
$order = array_merge($shippedCollection->getAllIds(),$processingCollection->getAllIds());
$order->getData('increment_id');
Hello check below code may be help you
$order_collection = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', array('in' => array('delivered_carrier','pending')));
echo count($order_collection->getAllIds());
I have a matrix of inputs boxes which contain prices for dates. If there is no price in the database for a particular date the input box displays 0. I have the following code which saves into the database the prices typed into the input boxes. It does not save all the 0 values only the new prices.
Which is fine. However I have now discovered an issue. If one of the inputs dislays a value from the database, say $10 and I want to set it now to 0, the code will not do it.
It will only save if the values and above 0. I have not been able to do this final check.
The conditions for saving are
1. If the value is numeric
2. If it is 0 and already has an entry in the database then save
3. If it has no value in the database and is greater than 0
4. If it is 0 and has no value in the database then do not save
if (isset($this->data['Rate'])){
// for each rate
foreach($this->data['Rate'] as $rate_id => $room){
// for each room type
foreach($room as $room_id => $room){
$price_for_today = isset($room['Price'][$key]) ? $room['Price'][$key] : 0;
// get existing availabilities is num (get this from previous date loop)
$today = ''.$date.' 00:00:00';
$conditions = array('Availability.date' => $today,'Availability.room_id'=>$room_id);
$this->Availability->contain();
$result = $this->Availability->find('all',array('order'=>'Availability.room_id ASC', 'conditions'=>$conditions));
$avail_id = $result[0]['Availability']['id'];
// check prices
$check_prices = "SELECT * FROM prices
WHERE rate_id = '".$rate_id."' AND availability_id = '".$avail_id."'";
$prices_result = $this->Availability->query($check_prices);
// if new prices > 0.00
if($price_for_today>0 && is_numeric($price_for_today)){
// better checking needed!
if($prices_result){
$setprices = "UPDATE prices SET price = '".$price_for_today."'
WHERE rate_id = '".$rate_id."' AND availability_id = '".$avail_id."'";
$update = $this->Availability->query($setprices);
} else {
$setprices = "INSERT INTO prices (price, availability_id, rate_id)
VALUES ('".$price_for_today."', '".$avail_id."', '".$rate_id."')";
$insert = $this->Availability->query($setprices);
}
}
//$errors[] = $setprices;
} // end rooms loop
} // end Rates loop
Your problem is in
> // if new prices > 0.00
> if($price_for_today>0 &&
> is_numeric($price_for_today)){
here you specify that $prices_for_today have to be >0, so if you had a price and want to put it 0 today then you will not do anything... You should use
if(($price_for_today>0 && is_numeric($price_for_today)) || (!empty($prices_result) && $price_for_today==0 && is_numeric($price_for_today))){
if you change it it will now enter in the if and do the change.
I sugest that you do NOT use the query function unless is extremely necesary. you should create a model for price (if you haven't done that already) and then use the associations (hasMany, HasOne, HABTM) or load the model directly in the controller with $this->loadModel('Price'). Then use a find 'all' as always with conditions and fields. This recomendation is to use cake as it was intended, not indispensable. Also the save, updatefield, read can be done if you do this... leaving the checks and everything to cake.