I have following issue.
I need to get rows from database where price is lower than given number.
I can do it this way:
DB::where('price', '<=', $given_number)->get();
but the problem is that inside price cell I have number with added signs like $ for dollars or Euro sign. I know how to parse the string in PHP to extract only numbers - filter_var($string, FILTER_SANITIZE_NUMBER_INT) , but how can I do this inside the query from Laravel? How can i pre-parse this 'price' attribute?
Is this what you mean?
$price = filter_var($given_number, FILTER_SANITIZE_NUMBER_INT);
$products = Product::where('price', '<=', $price)->get();
Is there more information that you left out?
Related
I got values stored in my database column field as value1,value2,value3,value4, so a simple_array column.
So i'm using Doctrine to make a search using this:
$searchQuery = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->andWhere("p.vals <= :value2")
->setParameter('value2', $request->query->get('value2'));
->orderBy("p.creationtime", 'DESC');
So expecting value2 is in the 2nd position of a simple array like value1,value2,value3, how can i ask QueryBuilder to select the second value in the string?
I think this query try to get all the values in p.vals, results are not right, shound select just one.
How can I select eg. the 2nd value in p.vals?
I believe you cannot access nth item of an array column using pure Mysql since the data is serialized, in order to do it I'd create a simple function
public function getItemFromArray(array $array, $index)
{
return isset($array[$index]) ? $array[$index] : null;
}
And if you want to find item with condition use
array_filter()
Trying to get the sum of a int field in one of my table should be pretty easy, unfortunately it is not as I'm getting different result whether I use Laravel, MySQL or Excel.
Laravel 5.4 gives me 20506:
Table::sum('field_name');
MySQL gives me 1830:
Select sum(field_name) from table;
And the data from the Excel sheet before importing it into the database:
Sum gives me 145689
Any idea? I tried to cast to integer before doing the sum but it doesn't work.
All the numbers are pretty big but don't contain comma or dot.
Examples of values I have to sum: (contain sometimes empty cells)
17906774
99630157
28581131
159551532
20312892
668928885
$query = YourModel::query();
$query->withCount([
'activity AS yoursum' => function ($query) {
$query->select(DB::raw("SUM(amount_total) as paidsum"))->where('status', 'paid');
}
]);
You can use laravel aggregate function SUM as :
$result = DB::table('table_name')
->select(DB::raw('SUM(field_name) as total_field_name'))
->get();
For more details you can follow:
https://laravel.com/docs/5.4/queries
Thanks
Try with
$result = DB::table(tablename)
->selectRaw('sum(column)')
->get();
If it still gives you wrong result, maybe wait for someone to give you a better answer. This is all I could think of.
I'm new to Laravel framework and I'm looking for how to write Query in order to get the absolute value(in short ignoring the + and - sign and only display number from database.
I know we can use Abs in order to get it. But I don't know how do it.
Take a look at my code:
$users = DB::table('transaction_details')->
Join('ledger','transaction_details.ledger','=','ledger.Name')->
groupBy('ledger.Name')->select(
'ledger.CrDr as CrDr',
'transaction_details.ledger as Name',
'transaction_details.amount as Debit',
'ledger.OpeningBalance as openingBalance'
)->get();
In above query i want ledger.openingBalance value to be absolute.
How do I do that?
You can use DB::raw for the ledger.openingBalance select column to avoid it from being quoted by the Query Builder:
DB::table('transaction_details')
->join('ledger','transaction_details.ledger','=','ledger.Name')
->groupBy('ledger.Name')
->select(
'ledger.CrDr as CrDr',
'transaction_details.ledger as Name',
'transaction_details.amount as Debit',
DB::raw('ABS(ledger.OpeningBalance) as openingBalance')
)->get();
I am developing in symfony 1.4 using Doctrine ORM. I can't create a NOT IN where clause using an array with ids. This is my code:
$results = Doctrine_Query::create()
->from('Asset a')
->where('a.id NOT IN (?)', implode(',', $ids))
->execute();
The sql for this query that is generated is this one:
WHERE (a.id NOT IN ('1,5,6,7,8,9,10,11,12,13,14,15,17,18,20,24,25,26,29,30,28'))
As you can see is treating the array filled with ids like an string. I tried also without the implode of the array but I get this error:
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
The array $ids containing the excluded ids is a plain numeric one.
I can't find out what is the correct syntax for this clause. Thanks
You have to use whereNotIn (old doc but still ok).
$results = Doctrine_Query::create()
->from('Asset a')
->whereNotIn('a.id', $ids)
->execute();
I cant figure out why when I try to use FORMAT function to limit number of decimal places in the results of MySQL query it doesn't work. Here is how my code looks like:
...some other options to join tables with some conditions....
$options['fields'] = array(
'MetricSim.sim_id',
'MetricSim.metric_id',
'FORMAT(MetricSim.value,3) AS value'
);
$metrics_sims = $this->Sim->find('all', $options);
If I don't use the FORMAT function I get all of the results as expected. But when I try to use it I just don't get value field in my results (the rest of the fields are in place).
Why do you want to use FORMAT in your query? You can use a Helper to format your data in your view.
For example, in your controller class you add:
var $helpers = array('Number');
and in your view, you can format the value like:
$number->format($metric_sims['MetricSim']['value']);
(See NumberHelper class)