I have a query where i need to use count and have a normal query.
Normal
select sum(total) as Total,AffID,user_id from Affiliates
group by AffID order by user_id ASC
And Then
foreach($result_array as $row)
{
echo $row['total']."<br/>";
echo $row['AffID']."<br/>";
echo $row['user_id ']."<br/>";
}
In Laravel i have tried
$affdata = DB::select('Affiliates')
->whereRaw('CompletedDate >= curdate()')
->groupBy('AffID')
->orderBy('user_id', 'ASC')
->sum('total');
->get();
foreach($affdata as $row)
{
echo $row->AffID ."<br>";
echo $row->total ."<br>";
}
But it seems to throw out an error. As i need to echo out the AFFID along with calculating the Total
Update your query to this.
$affdata = DB::table('Affiliates')->select(DB::raw('sum(total) as total'), 'AffID', 'user_id'))
->whereRaw('CompletedDate >= curdate()')
->groupBy('AffID')
->orderBy('user_id', 'ASC')
->sum('total');
->get();
This is the correct query
$affdata = DB::table('Affiliates')->select(DB::raw('sum(total) as total'), 'AffID', 'user_id')
->whereRaw('CompletedDate >= curdate()')
->groupBy('AffID')
->orderBy('user_id', 'ASC')
->get();
Related
I have MySql Query :
select * from transactions where request_shipping_date = '2020-11-25' and status = 'paid' or 'on_process' or 'finish';
and I got 44 Rows
and I Want Convert to Eloquent Model :
$request_shipping_date = Carbon::now()->addDays(1)->format('Y-m-d');
$data = Order::with('order_details')
->where('request_shipping_date', 'like', $request_shipping_date.'%')
->Where('status', 'paid')
->orWhere('status', 'on_process')
->orWhere('status', 'finish')
->get();
and i got : 24025 Rows
Is there any wrong ?
The no of rows retrieved changes because of the like operator when comparing dates
Try using the below
$request_shipping_date = Carbon::now()->addDays(1)->format('Y-m-d');
$data = Order::with('order_details')
->whereDate('request_shipping_date', $request_shipping_date)
->Where('status', 'paid')
->orWhere('status', 'on_process')
->orWhere('status', 'finish')
->get();
You could also rephrase the eloquent query as
$request_shipping_date = Carbon::now()->addDays(1)->format('Y-m-d');
$data = Order::with('order_details')
->whereDate('request_shipping_date', $request_shipping_date)
->whereIn('status', ['paid', 'on_process', 'finish'])
->get();
I trying to convert MySQL query to Laravel eloquent, but not getting the right output.
MySQL query -
Select contents.con_type, contents.operation, contents.ecu, contents.ecu_sub, COUNT(*)
from contents
group by operation, con_type, ecu, ecu_sub
order by operation asc, con_type asc
MySQL query to Laravel elequent -
$content = Content::query();
if ($request->input('total') == '1') {
$content = $content->select('con_type', 'operation', 'ecu', 'ecu_sub')
->groupBy('operation', 'con_type', 'ecu', 'ecu_sub')
->orderBy('operation', 'asc')
->orderBy('con_type', 'asc')
->get()->count();
}
The output I trying to get:
Thanks in advance.
Test
$content = Content::query();
if ($request->input('total') == '1') {
$content = $content->select(DB::raw('con_type, operation, ecu, ecu_sub, count(*) as `count`')
->groupBy('operation', 'con_type', 'ecu', 'ecu_sub')
->orderBy('operation', 'asc')
->orderBy('con_type', 'asc')
->get();
}
I have a table column named created_at type datetime.
and I want to use groupby for created_at with date only and without time.
Here's my existing code :
$data = BloodPressureHistory::where('user_id', $id)
->groupBy('created_at')
->orderBy('created_at', 'DESC')
->paginate(7);
Use groupBy(DB::raw()) and DATE_FORMAT like this:
$data = BloodPressureHistory::where('user_id', $id)
->groupBy(DB::raw('DATE_FORMAT(created_at,"%Y-%m-%d")'))
->orderBy('created_at', 'DESC')
->paginate(7);
$data = BloodPressureHistory::where('user_id', $id)
->groupBy(function ($item){
return \Illuminate\Support\Carbon::parse($item->created_at)->format('Y-m-d');
})
->orderBy('created_at', 'DESC')
->paginate(7);
How to query date month and year between from_date and to_date?
What I tired:
$posts = my_table::whereYear('from_date', '>=', $year)
->whereMonth('from_date', '>=', $month)
->whereYear('to_date', '<=', $year)
->whereMonth('to_date', '<=', $month)
->get();
Query I want to execute:
SELECT * FROM my_table
WHERE (MONTH(from_date)>=2 AND (Year(from_date)>=2019)
AND (MONTH(to_date)=<4 AND (Year(to_date)=<2019)
I think this is what you might be looking for:
$posts = my_table::whereRaw("(MONTH('from_date') > 2 AND YEAR('from_date') >= 2019)")
->whereRaw("(MONTH('to_date') < 4 AND YEAR('to_date') <= 2019)")
->get();
Here's an example.
You can use the eloquent method too.
$result = my_table::where(function($query){
$query->where(DB::raw('MONTH(from_date)'), '>=' , 2)
->where(DB::raw('Year(from_date)'), '>=' , 2019);
})
->where(function($query){
$query->where(DB::raw('MONTH(to_date)'), '<=' , 4)
->where(DB::raw('YEAR(to_date)'), '<=' , 2019);
})
->get();
you may also do it in this way.
return Post::whereRaw("STR_TO_DATE('from_date', '%M%Y') > STR_TO_DATE('022019', '%M%Y')")
->whereRaw("STR_TO_DATE('from_date', '%M%Y') < STR_TO_DATE('042019', '%M%Y')")
->get();
I'm having a bit of a problem with join SUM in fuel php.
When I use it like this
$query = DB::select(
'stream_post.*',
'SUM(stream_comment.comment_stream_id)'
)->from('stream_post');
$query->join('stream_comment', 'LEFT');
$query->on('stream_post.stream_id', '=', 'stream_comment.comment_stream_id');
$query->join('users_metadata');
$query->on('stream_post.user_id', '=', 'users_metadata.user_id');
$query->limit(10);
$query->order_by('stream_id', 'DESC');
$result = $query->execute();
if(count($result) > 0) {
foreach($result as $row)
{
$data[] = $row;
}
return $data;
}
I get this error
Column not found: 1054 Unknown column 'SUM(stream_comment.comment_stream_id)' in 'field
What do im doing wrong?
You need to use the expr function to create an expression in the select statement
$result = DB::select(DB::expr(' SUM(stream_comment.comment_stream_id) as count'))->from('stream_post')->execute();
Documented here http://docs.fuelphp.com/classes/database/usage.html