$notif = DB::table('notifications')->orderBy('updated_at', 'desc')->get();
Above code is to get the values in descending order according to the default column updated_at in laravel 5. Although that will display the values ascending order. I want to display the most recently updated one at first.How to correct that?
Looks like you are viewing the results from phpmyadmin or something similar. The code
$notif = DB::table('notifications')->orderBy('updated_at', 'desc')->get();
will give you results in your web application.
It'll not store the records in descending order in the database as you are expecting.
Your statement $notif = DB::table('notifications')->orderBy('updated_at', 'desc')->get(); definately gives you the right sorting. As #linuxartisan already mentioned: are you sure your tool you are using for showing the database uses the right sort direction? It seems 11, 12 in your first column is the the id and the sort order is using id ascending
Related
I'm used to compare 2 data, one data has an id and the other data is the one that needs comparing to get the matching datas. see code below.
DB::table('requests')->where('reqItem',$inventory->invItem)->get();
The code shown above displays all requests information that equals to the compared inventory item.
Now what i want to do now is to compare 2 tables without any id (ex. $inventory->invItem). I don't know how to ask this question but i hope you get what i mean. The figure below shows the way i wanted it to be.
You can run this query. Here, 'inventory' is the name of second table (change it accordingly)
DB::table('requests')
->join('inventory', 'requests.reqItem', '=', 'inventory.invItem')
->select('inventory.reqItem')
->get();
I need to sort a very long list fields in Power BI that I need in a special order - not the default sort by name/number.
My dataset looks like this:
and I created another table to use for sorting:
I am stuck from here. I think I need to create a relation between the to tables and then use it to sort the field list. But I do not know where to start.
The order I have now looks like this:
Assuming you're sorting in Power Query, then:
1 Sort table SortOrder by column SortOrder:
= Table.Sort(#"Previous Step",{{"SortOrder", Order.Ascending}})
2 Sort table Data by list SortOrder[ColumnName]:
= Table.Sort(#"Previous Step",SortOrder[ColumnName])
EDIT
It seems you're actually asking how to change the order in which fields are displayed in the Fields Pane
This is currently not possible - fields are always displayed in alphabetical order.
You may vote for this feature request: https://ideas.powerbi.com/forums/265200-power-bi-ideas/suggestions/18355429-use-field-order-as-ordered-in-source-query
The only workaround just now would be to rename your fields so they display in your required order - eg:
01_ClmName_A
02_ClmName_C
03_ClmName_E
04_ClmName_B
05_ClmName_Q
06_ClmName_F
I have implemented laravel dataTable as a service.
The initial two columns are actual id and names so, I am able to sort it asc/desc after the table renders.
But the next few columns renders after performing few calculations, i.e. these values are not fetched directly from any column rather it is processed.
I am unable to sort these columns where calculations were performed, and I get this error. And I know it is looking for that particular column for eg outstanding_amount which I don't have in the DB, rather it is a calculated amount from two or more columns that are in some other tables.
Any Suggestions on how to overcome this issue?
It looks like you're trying to sort by values that aren't columns, but calculated values.
So the main issue here is to give Eloquent/MySql the data it needs to provide the sorting.
// You might need to do some joins first
->addSelect(DB::raw('your_calc as outstanding_amount'))
->orderBy('outstanding_amount') // asc can be omitted as this is the default
// Anternative: you don't need the value sorted by
// Don't forget any joins you might need
->orderByRaw('your_calc_for_outstanding_amount ASC')
For SQL functions it'll work as follow
->addSelect(DB::raw('COUNT(products.id) as product_count'));
->orderByRaw(DB::raw('COUNT(products.id)'),'DESC');
How to fetch specific value first then ascending order using Laravel Eloquent?
I want to show India first then other countries in ascending. I am using
$country_data= country::orderBy('country_id','India')->get();
You can user orderByRaw with mySQL FIELD function
So try this:
$country_data= country::orderByRaw("FIELD(country_id,'India') DESC")->orderBy("country_id","asc")->get();
function getIdModelsSliderJuwels(){
$collection = Mage::getModel("catalog/product")->getCollection();
$collection->addAttributeToFilter("attribute_set_id", 27);
$collection->addAttributeToSelect('modellijnen');
// $collection->setRandomOrder();
// $collection->getSelect()->limit( 5 );
return $collection;
}
Hi there,
I'd like to know how to set a limit to your query running in Magento because
$collection->getSelect()->limit( 5 ); doesn't work.
Also how to select randomly, $collection->setRandomOrder(); also doesn't work.
txs.
setRandomOrder does not work for collections of products, only for related products. You'll have to add it yourself with this code:
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
A shortcut for setting both page size and number at the same time is:
$collection->setPage($pageNum, $pageSize);
As clockworkgeek said, use the $collection->getSelect()->order(...) method to randomize the order. To limit it to just $n number of items you can also use
$collection->getSelect()->limit($n);
try to use
$collection->setPageSize(5)->setCurPage(1);
Using ORDER BY RAND() to return a list of items in a random order will require a full table scan and sort. It can negatively affect performance on large number of rows in the table.
There are several alternative solutions possible of how to optimize this query. Magento provides a native solution for that.
The orderRand() method of Varien_Db_Select and the database adapter allows to specify a random order and leverage index for ORDER BY. Specify a name of some integer indexed column to be used in the ORDER BY clause, for example:
$collection->getSelect()->orderRand('main_table.entity_id');
See Varien_Db_Adapter_Pdo_Mysql::orderRand() for implementation details.