Query model to get latest results first - laravel-5.4

How to query Model say Post to get latest post first. Descending order.
My question is that is there any in built method exists something like latestFirst() in Laravel 5.4 to query model?

You can get all the latest post by ordering the posts by created_at column like this:
$latestPost = Post::orderBy('created_at', 'desc')->first();

You can also use latest(). For e.g:
$post = Post::latest()->first();
From the docs: The latest and oldest methods allow you to easily order results by date. By default, result will be ordered by the created_at column. Or, you may pass the column name that you wish to sort by

Related

Select all but the most recent row in a database using Eloquent in Laravel

I want to select all rows from a mysql table except for the most recent one by using Eloquent in Laravel.
I currently do this to get the most recent entry only, but wish to do the reverse:
Table::orderBy('created_at', 'desc')->first();
Any idea how I do this?
You can make use of laravel collections shift method.
$data = DB::table('your_table')->orderBy('created_at', 'desc')->get();
$data->shift(); //which removes the first element. now $data has all other values except for first one.
you can simply skip the first one from the results:
Table::orderBy('created_at', 'desc')->skip(1)->get();

How to Fetch data in Descending order in Laravel 5.6 using provider

I'm fetch data from database using App\Provider ad show data in home page and admin panel. I want to sort data in descending order.
Code in app\provider
View::composer('*',function($view){
$view->with('services', Service::all());
});
If you want to sort data in Descending and Acesending order in Laravel 5.6 it just simple code like this:
View::composer('*',function($view){
$view->with('services', Service::latest()->get());
});
Latest function is equal orderBy('created_at', 'DESC'); and get function help to get all data from database.
This code also use ('services', Service::latest()->get()) fetch data in descending order in controller.
Thank You
if you have any question you can ask me feel free.

Regarding a use case on queries in Druid

I have a use case in druid where real-time data comes in the format like,
task{
taskno;
category;
}
Here category can be "assigned" or "unassigned".Suppose tasks with the following came,
taskno:1,category:"assigned"
taskno:2,category:"unassigned"
taskno:3,category:"assigned"
taskno:4,category:"assigned"
Here if I perform a query with filter as "category" with count(*) on it ,I will get result as;
assigned:3
unassigned:1
Now a new event comes with
taskno:2,category:"assigned"
I want in such a way that the query results
assigned:4
unassigned:0
Is there any way I can do like this using Javascript UDF or something in druid?
Thanks.
You can first filter your query on task and than filter it on category with order as desc and limit 1. It should give you the desired result.
And in your function where you getting the results you can addup the counts, or you can write a post aggregation function to do the same in druid.

Sorted array with specific items on top using Zend_Db_Select order()

I am trying to modify a plug-in that, among other things, alphabetically sorts an array from a mysql database. The part of the relevant PHP file that does the sorting looks like this:
$select->order("$alias.name ASC");
I want to continue to sort the items in ascending order, but I want to keep a specific item at the top (out of its alphabetical order).
From the information here it looks like the way to do this in MySQL would be simple, just:
ORDER BY name = 'Library' desc,
name asc;
But even studying the information here, I can't quite translate this into the order() function. I have tried:
$select->order("$alias.name ='Library' DESC","$alias.name ASC")
But it seems that it does not work quite that way.
Alright, found the answer-
I needed to use Zend_Db_Expr(). In case it is helpful to anyone else, this was solved like so:
$select->order(array(
new Zend_Db_Expr("$alias.name = 'Library' DESC"),
new Zend_Db_Expr("$alias.name ASC")
));

How to query "where" something equals something

Right so at the minute my query looks like this:
$subcategory = Subcategory::find()->asArray()->all();
this basically grabs all the data from my subcategory table and stores it into $subcategory.
However i want to find specific data. For example in my subcategory table i have a column called subcategory_id.
Now i want to pull back all the subcategories where subcategory_id = $firstcategory(This will be a number)
Im guessing that it is something like this, however it is not working. Any ideas on how to do this query?
$subcategory = Subcategory::find()->asArray()->all()->where('subcategory_id'
== $firstcategory);
There is no need to guess, just read the docs. Here is one of the ways to do it:
$subcategory = Subcategory::find()
->where(['subcategory_id' => $firstcategory]);
->asArray()
->all();
Note that order of using all() is crucial, once it's called you get the array of results and you can not modify query anymore.