How to make laravel 5.2 pagination work(count) on just main table records not include the join tables - laravel

$employees = DB::table('users')
->leftJoin('assigned_branches','assigned_branches.user_id','=','users.id')
->leftJoin('assigned_geo_infos','assigned_geo_infos.id' ,'=','assigned_branches.region_branch_id')
->leftJoin('user_customers','user_customers.user_id','=','assigned_branches.user_id')
->leftJoin('customers','customers.id','=','user_customers.customer_id')
->whereIn('assigned_geo_infos.project_id',$assigned_projects_ids)
->where([['users.office_staff',0],['users.active',$filter]])
->select('assigned_geo_infos.*','assigned_geo_infos.id as info_id','assigned_geo_infos.name as info_name','assigned_branches.*','assigned_branches.level as region_branch_level','users.*','customers.customer_name')
->paginate(15);
So here i want to pagination process or calculation on just users table not on other table means join table.According to users total count should be 2 but due to join its is giving total page 8. Or is there any other solution in which i can get all join table records as sub array of main table record array.

Use groupBy() method, to group the results:
$employees = DB::table('users')
->leftJoin('assigned_branches','assigned_branches.user_id','=','users.id')
->leftJoin('assigned_geo_infos','assigned_geo_infos.id' ,'=','assigned_branches.region_branch_id')
->leftJoin('user_customers','user_customers.user_id','=','assigned_branches.user_id')
->leftJoin('customers','customers.id','=','user_customers.customer_id')
->whereIn('assigned_geo_infos.project_id',$assigned_projects_ids)
->where([['users.office_staff',0],['users.active',$filter]])
->select('assigned_geo_infos.*','assigned_geo_infos.id as info_id','assigned_geo_infos.name as info_name','assigned_branches.*','assigned_branches.level as region_branch_level','users.*','customers.customer_name')
->groupBy('users.id')
->paginate(15);

Related

laravel 8 use orderBy and withMax for order tables with another table

i want to orderby products with max price from details table.
products one to many relation with details table.
in laravel documentation we have aggregate methods like withMax ,withMin and withCount
. These methods will place a {relation}_{function}_{column} attribute on your resulting models.
now we only need to orderBy with this results.
$products = Product::withMax('details', 'price')
->orderBy('details_max_price' , 'desc')
->paginate(15);

How to use order by query both with current table column and relationship table column in laravel?

i am trying to fetch record with order by query but situation is this that i have to use order by both on current table column and relationship table column in laravel . I tried this
$consignments = Consignment::where('delivery_run_id', $id)->whereIn('status', [
'In Warehouse',
'With On Forwarder',
'In Transit (Warehouse->Delivery)',
'Awaiting Pickup'
])->with(['consignment_run_sheet' => function ($query) {
$query->orderBy('run_sheet_id');
}])->orderBy('delivery_date', 'DESC')->get();
$deliveryRuns = DeliveryRun::all();
How I can achieve it?
it will order just the relation items in this way. solution is use subquery or joins. useing Subquery is like this if assume the modal for consignment_run_sheet relation is ConsignmentRunSheet and the relation is belongsTo:
$consignments = Consignment::where('delivery_run_id', $id)->whereIn('status', [
'In Warehouse',
'With On Forwarder',
'In Transit (Warehouse->Delivery)',
'Awaiting Pickup'
])->with('consignment_run_sheet')
->orderBy(ConsignmentRunSheet::select('delivery_date')
->whereColumn('consignments.id', 'consignment_run_sheet.consignment_id'), 'DESC')
->get()
source:
https://reinink.ca/articles/ordering-database-queries-by-relationship-columns-in-laravel

Eloquent methods lumen

I have belongsToMany relationship between items and vehicle.
items can be assigned to multiple vehicles. same vehicle can b assigned to multiple items. so my pivot table item_vehicle have extra column date which will show that when vehicle is assigned to item.
here is my query.
select `items`.`id`, `items`.`name`, `items`.`area` as `total_area`,
`item_vehicle`.`date`, `vehicles`.`name` as `vehicle_name`,
SUM(parcel_vehicle.area) as processed_area
from `parcels`
inner join `item_vehicle` on `item_vehicle`.`p_id` = `items`.`id`
inner join `vehicles` on `item_vehicle`.`t_id` = `vehicles`.`id`
where `item_vehicle`.`date` < '?' and `items`.`processed` = ? and `vehicles`.`name`=?
group by items.id
what will be the eloquent way of doing this
Item::with(['vehicle'=>function($q){$q->wherePivot('date','<','2019/2/12');}])->whereHas('vehicle',function($q){$q->where('vehicles.id','2');})->where('processed',1)->where('id',4)
->get();
my concerns is it should run only one query
$parcels = Parcel::join('item_vehicle', 'item_vehicle.pid', '=' ,'items.id')
->join('vehicles', 'vehicles.id', '=' ,'item_vehicle.t_id')
->where('item_vehicle.date', '<', $date)
->where('items.processed', $processed)
->where('vehicles.name', $vehicleName)
->select(
'items.id',
'items.name',
\DB::raw('items.area as total_area'),
'item_vehicle.date',
\DB::raw('vehicles.name as vehicle_name'),
\DB::raw('SUM(parcel_vehicle.area) as processed_area')
)
->groupBy('items.id')
->get();
However, you have non-aggregated columns in select and you are doing group by. To make this work you might need to disable mysql's only_full_group_by mode

Laravel join with limit

I have the following working for laravel paginate
$query = DB::table('clients')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', 'clients.username', '=', 'recharge.soldto');
I want to join some values from two tables radusergroup and recharge. radusergroup always return one row as it has only one row stored whereas recharge table return multiple rows. I want only one row returned from recharge table which is latest entry.
Right now its return all the possible rows from recharge table and showing it on paginated view.
This is Laravel Official documentation
DB::table('clinets')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', function ($leftJoin) {
$leftJoin->on('clients.username', '=', 'recharge.soldto')
->where('recharge.create_date', '=', DB::raw("(select max(`create_date`) from recharge)"));
})
->get();
this is the case if you have create_date column in your table, if you haven't got it I strongly recommend to create that column.

laravel 4.2 and join function

$options = table::join('table1', function($join) {
$join->on('table1.table_id','=','table.id');
$join->take(1);
})->get();
Is there a way to do this, i get error on take function.
the table and table1 has one to many relation, and i'd like to have on join just the first record of table1.
Thanks
Vincenzo

Resources