This is some sorting:
I want to list 20 best posts based on "views".
then, sort it based on "created_at".
How to do it?
Heres my current code (which work, but doesnt sort by "created_at"):
$this->data['today_post'] =
Posts::orderBy('views', 'desc')
->orderBy('created_at', 'desc')
->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')
->limit(20)->get();
You can write this. Hopefully this will solve your problem
$this->data['today_post'] =
Posts::orderBy('views', 'desc')
->orderBy('created_at', 'desc')
->whereDate('created_at', Carbon::today()->toDateString())
->limit(20)
->get();
Also add use Carbon\Carbon; at the beginning of your controller.
Get a collection from database first
$x = Posts::whereRaw('created_at >= NOW() - INTERVAL 1 DAY')->orderBy('views', 'desc')->limit(20)->get();
Then sort the collection
Descending:
$this->data['today_post'] = $x->sortByDesc('created_at');
Ascending:
$this->data['today_post'] = $x->sortBy('created_at');
You can simply sort the result collection. Keeping your code, add the following:
$this->data['today_post'] = $this->data['today_post']->sortBy('created_at');
Related
I want to get all the data from SALES and PURCHASES table but I want to show product name for the single time and also want to show the sum of its quanitity..
How can i do this in laravel 8 ?
What will be the query ??
$stock = DB::table('products')
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('product_id')
->get();
I dont have your whole query but use havingRaw to find the duplicates.
$dups = DB::table('tableName')
->select('col1_name','colX_name', DB::raw('COUNT(*) as `count`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('col1_name', 'ColX_name')
->havingRaw('COUNT(*) > 1')
->get();
Maybe something like that
$stock = DB::table('products')
->select('sales.*','purchases.*','products.name', DB::raw('products.name as `NbProduct`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('sales.id', purchases.id)
->get();
How can I filter out only results when total is greater than n?
other words only IPs with more wisits than n (say 500)
I tried ->where('total','>',500) but it didn't work
Thank you
$visits = DB::table('visits')
->select('ip_address',DB::raw('count(*) as total'))
->where('timestamp', '>=',\Carbon\Carbon::now()->startOfDay())
->groupBy('ip_address')
->orderBy('total', 'desc')
->get();
WHERE cannot be used on grouped item (such as count(*)) whereas HAVING can.
You can refer WHERE vs HAVING question to understand more details,
You have to use having
->having('total', '>', 100)
optionally in your case you can use havingRaw
->havingRaw('count(*) > 2500')
->havingRaw('total > 2500')
ref : https://laravel.com/docs/5.6/queries
I have one problem in my query is CURDATE() not working in my laravel 5.2
AbsenController#index
$now = time();
$absen = Absen::with('siswa')->where('level', '=', 'Siswa', 'AND',
'created_at', '<=', 'CURDATE()')->get();
return view('absen.index')->with('data', $absen);
and this is record in my Absen Table
I am not familiar with your where sytnax. Try including separate terms for each of the two conditions in your WHERE clause:
$absen = Absen::with('siswa')
->where('level', '=', 'Siswa')
->where('created_at', '<=', DB::raw('curdate()'))
->get();
As a variant, you could also use whereRaw() to handle the condition involving CURDATE():
$absen = Absen::with('siswa')
->where('level', '=', 'Siswa')
->whereRaw('created_at <= curdate()')
->get();
Conditions are ANDed together by default, which is the relationship you have between your two conditions. Look into orWhere() if you want to OR together conditions in the WHERE clause.
$now = time();
$absen = Absen::with('siswa')->where('level', 'Siswa')->where('created_at','<=',\Carbon\Carbon::now())->get();
return view('absen.index')->with('data', $absen);
I want to sort my Laravel query builder results on a custom column (concat of first_name and last_name).
What I have done is-
$summary = DB::table('service_rating')
->join('partners', 'partners.id', '=', 'service_rating.partner_id')
->join('users', 'users.id', '=', 'partners.user_id')
->select(
DB::raw("CONCAT( users.first_name,' ', users.last_name) as lawn_pro"),
DB::raw ('AVG(service_rating.rating) as rating'),
DB::raw ('COUNT(service_rating.rating) as jobs'),
DB::raw ('SUM(service_rating.rating) as payout')
)
->where('customer_id', '=', Auth::user()->id)
->whereRaw('service_rating.created_at >= DATE(NOW()) - INTERVAL '.$no_of_day_to_show.' DAY')
->groupBy('service_rating.partner_id')
->orderBy('lawn_pro', 'asc');
So, I am getting error for this line -
->orderBy('lawn_pro', 'asc');
And error is like this-
Can anyone please help ?
Apparently you are using the count() function on your query, this ignores the select attributes because we only want to know the count of the rows. Because of this, lawn_pro is not available in the query.
I would suggest to execute the query and then count the available rows.
$rows = $summary->get();
$count = count($rows);
I'm trying to select last visit date in php (Laravel). First I order by descending and then select the maximum date. Can any body help me?
Here is my unsuccessful code:
$getlast = DB::table('tr_visit')
->join('tm_child','tm_child.Child_ID','=','tr_visit.Child_ID')
->where('tm_child.Child_Name', 'LIKE', '%'.$input.'%')
->where('tm_child.Child_ID','=','CH001')
->orderBy('tr_visit.Visit_Date', 'desc')
->select(DB::raw('max(tr_visit.Visit_Date),Visit_Date'))
->get();
Try this
$getlast = DB::table('tr_visit')
->join('tm_child','tm_child.Child_ID','=','tr_visit.Child_ID')
->where('tm_child.Child_Name', 'LIKE', '%'.$input.'%')
->where('tm_child.Child_ID','=','CH001')
->orderBy('tr_visit.Visit_Date', 'desc')
->select(DB::raw('max(tr_visit.Visit_Date),Visit_Date'))
->first();
If you are already ordering by the date descending, there is no need to select the maximum. The first row will be the maximum date.
->orderBy('tr_visit.Visit_Date','desc')
->first();
Should be all you need.
If you want to order by date in descending order,then you have to use DATE like this
->orderBy("DATE('tr_visit.Visit_Date')",'desc')
->first();