I am using Laravel , I have got some users and appointments,
some of the users doest have appointments
I need to view both of my users who has appointments and not
also, I have added a filter that is sorted by date,
while sorting I need to sort the appointment by its date and need to display the rest as it is,
is there a way to do this, this is my code
this is my code which i get the all appointments
$appointments = $this->conn->table('appointments')
->select('*','response->date','users.id as useid','appointments.id as app_id','appointments.created_at as created_at','appointments.response->date as app_data')
->join('users', 'appointments.user_id', '=', 'users.id')
this is what i am trying to do
$appointments = $appointments
->whereNull('appointments.response->datetime')
->orWhere(DB::raw( "to_char((appointments.response::json->>'datetime')::timestamp, 'MM/DD/YYYY')" ), '>=', $start_date_format)
->orWhere([DB::raw( "to_char((appointments.response::json->>'datetime')::timestamp, 'MM/DD/YYYY')" ), '<=', $end_date_format])
;
this code display ass the users but when I try to filter this won't work
Related
I'm working in laravel and I need to filter the users who orders with certain amount of money in orders
so I used to filter the orders count
User::where('type','client')
->has('orders', '>=', $min_orders)
->has('orders', '<=', $max_orders)
->withCount('orders')
->paginate(25)
;
Try this one
$users = User::filter(function ($user) use ($min_orders, $max_orders) {
return ($user->orders()->sum('price') >= $min_orders) && ($user->orders()->sum('price') <= $max_orders);
})->get();
Haven't tested, but I think it should work
If you want SUM with User info then you need to GROUP BY on user info. And If you want the SUM on the Many side table, then you have to JOIN to that table. Assuming you have orders.amount field, you can try this :
<?php
User::select('user.id', 'email', 'name', 'type', \DB::raw('SUM(orders.amount) as amount_sum'))
->where('type','client')
->join('orders', 'orders.user_id', '=', 'user.id')
->has('orders', '>=', $min_orders)
->has('orders', '<=', $max_orders)
->withCount('orders')
->groupBy('user.id', 'email', 'name', 'type')
->havingRaw('amount_sum > ?', [50])
->orderBy('amount_sum', 'desc')
->paginate(25);
This way you can paginate directly into One single query results. But remember that each User column you want to use in SELECT has to be used in GROUP BY too.
I have a project which has management of my investments, that is, it takes my daily statement and saves it in my project so that I can see how much it is yielding, in summary I have a table that are these values daily, I can already generate the information separated by day and it works perfectly (in fact it is a little more complex than this what I do but the basis is this);
But now it's getting bad to see when I get a very large date range for example 1 year, it lists every day, I wanted a way to group by month or week,
Thanks in advance for any help.
$rows = AtivosExtrato::select('titulo_id', DB::raw('SUM(valor_bruto_atual) AS valor'), 'data_imports.data_import as created_at')
->join('titulos','titulo_id', '=', 'titulos.id' )
->join('representantes','representante_id', '=', 'representantes.id' )
->join('data_imports','data_import_id', '=', 'data_imports.id' )
->where('user_id', Auth::user()->id)
->whereBetween('data_imports.data_import', [$request->input('start_date'), $request->input('end_date')])
->groupBy('titulos.nome_titulo')
->groupBy('data_imports.data_import')
->orderBy('data_import')
->orderBy('titulos.nome_titulo')
->get();
Briefly explaining what each eloquent information is:
AtivosExtrato: model where the daily income information is;
1) join: foreign table for the names of the titles
2) join: foreign table for the broker's name
3) join: table that saves the date of the import and relates to the id in the asset tableExtrato, it has a function to reduce the weight in the time of the searches and to gain performance.
where: limiting to the user in question
WhereBetween: limiting to date range
1) groupBy: Grouping by titles
2) groupBy: grouping by date of import
Table structure:
ativos_extrato
ativos_extratos foreign key
data_imports
Solution:
$rows = AtivosExtrato::select(
'titulo_id',
DB::raw('SUM(valor_bruto_atual) AS valor'),
'data_imports.data_import as created_at',
DB::raw('WEEK(data_imports.data_import) AS weeknumber')
)
->join('titulos','titulo_id', '=', 'titulos.id' )
->join('representantes','representante_id', '=', 'representantes.id' )
->join('data_imports','data_import_id', '=', 'data_imports.id' )
->where('user_id', Auth::user()->id)
->whereIn('ativos_extratos.data_import_id',
DataImport::Select(DB::raw('max(ID)'))
->whereBetween('data_import', [$request->input('start_date'), $request->input('end_date')])
->groupBy(db::raw('Week(data_import)')) )
->whereBetween('data_imports.data_import', [$request->input('start_date'), $request->input('end_date')])
->groupBy('titulos.nome_titulo')
->groupBy('weeknumber')
->orderBy('data_import')
->orderBy('titulos.nome_titulo')
->get();
Add DB::raw(WEEK(data_imports.data_import) AS weeknumber) and then replace ->groupBy('data_imports.data_import') with ->groupBy('weeknumber') and the same with MONTH() function if you want to group by month: add another select column DB::raw(MONTH(data_imports.data_import) AS monthnumber) and replace ->groupBy('data_imports.data_import') with ->groupBy('monthnumber'). So the whole Eloquent query with week grouping would be:
$rows = AtivosExtrato::select('titulo_id', DB::raw('SUM(valor_bruto_atual) AS valor'), 'data_imports.data_import as created_at', DB::raw('WEEK(data_imports.data_import) AS weeknumber'))
->join('titulos','titulo_id', '=', 'titulos.id' )
->join('representantes','representante_id', '=', 'representantes.id' )
->join('data_imports','data_import_id', '=', 'data_imports.id' )
->where('user_id', Auth::user()->id)
->whereBetween('data_imports.data_import', [$request->input('start_date'), $request->input('end_date')])
->groupBy('titulos.nome_titulo')
->groupBy('weeknumber')
->orderBy('data_import')
->orderBy('titulos.nome_titulo')
->get();
I am trying to get sum of amount of all users group by month.But if I add sum then query not work.It return amount log as blank array But without sum query return all logs data as monthly.
My query
$reports = $user->whereHas('groups', function ($query) use($acceptedGroup) {
$query->whereIn('groups.name',$acceptedGroup);
})->with(
array(
'amountLogs' => function($query) use($year){
$query
->select(
DB::raw('sum(amount) as total')
)
->whereYear('created_at','=', $year)
->groupBy(DB::raw("MONTH(created_at)",'user_id'))->get();
})
);
If I remove
->select(
DB::raw('sum(amount) as total')
)
Then query works
If you creating a specific select on a relationship in your query, you also need to include the foreign key to the related table (in your case, probably the users table)
->select(
'user_id',
DB::raw('sum(amount) as total')
)
This allows Eloquent to relate the records after loading from the database.
i have a comanies table like this in mysql.it has 3 columns. is_verified,is_platinum,platinum_start_date, end_date.
on the frontend, the data is showing in paginate format so that now I want to get the companies list sorted in only one variable.
just to show companies first verified then non verified then platinum etc
All Platinium and verified companies
All Platinium
All verified companies
All no verified and no platinum user.
I have tried by "orderBy","Sorting" etc but by using individual variable
$companies = DeveloperCompanies::where('is_approved', '=',
1)->orderBy('created_at', 'desc')->paginate(9);
here is a table format
use orderBy four times for each column
$companies = DeveloperCompanies::where('is_approved', '=', 1)
->orderBy('is_verified', 'desc')
->orderBy('is_platinium', 'desc')
->orderBy('platinium_start_date', 'desc')
->orderBy('platinium_end_date', 'desc')
->paginate(9);
I'm tring to retrieve the totals of items from a table but can't get my head around it.
What I'm trying to achieve is this:
I have a table 'users' with the following columns:
Admin - yes/no
Moderator - yes/no
I am trying to retrieve the total of the items in the table but also the total of items in the table with admin = 'yes' OR moderator = 'yes'. The reason for this is to display the totals in one table.
Below is my sample code:
$date = new DateTime('tomorrow -360 month');
$usersPerDay = User::select(array(
DB::raw('DATE(`created_at`) as `date`'),
DB::raw('COUNT(*) as `count`'),
this is where I have the problem - Is it possible to do something like
this
DB::raw('COUNT(*) as "type" WHERE admin= "yes" OR moderator= "yes"')
))
->where('created_at', '>', $date)
->groupBy('date')
->orderBy('date', 'DESC')->get();