groupby working in 3 tables and i need only one table - laravel-5

le_maincategory.mc_name affected all table. I need one table using groupby option
Model
public static function mainmenucategory()
{
return DB::table('le_maincategory')
->join('le_addcategory', 'le_addcategory.cat_id', '=', 'le_maincategory.mc_name')
->join('le_add_subcategory', 'le_add_subcategory.main_cat_id', '=', 'le_maincategory.mc_name')
->join('le_second_category', 'le_second_category.main_cat_id', '=', 'le_maincategory.mc_name')
->where('le_maincategory.mc_status', '=', '1')
->where('le_addcategory.status', '=', '1')
->groupBy('le_maincategory.mc_name')
->get();
}

Related

Laravel eloquont query takes long time to fetch data

I have a query just to retrieve the data, when I try to fetch 10k data from below query it will take more than 1.7mins. It that reasonable or how can I improve this time.
Vehicles::leftJoin('vehicle_details', 'vehicle_details.id', 'vehicles.vehi_details_id')
->leftJoin('vehicle_extras', 'vehicle_extras.vehicle_id','vehicles.id')
->leftJoin('reasons', 'reasons.vehicle_id', 'vehicles.id')
->leftJoin('revised_vehicles', 'revised_vehicles.vehicle_id','vehicles.id')
->leftJoin('makes', 'makes.model_id', 'vehicle_details.model_id')
->leftJoin('bidder_invoices', 'bidder_invoices.vehicle_id', '=', 'vehicles.id')
->leftJoin('bidding_views', 'bidding_views.vehicle_id', '=', 'vehicles.id')
->leftJoin('bidder_prs', 'bidder_prs.vehicle_id', '=', 'vehicles.id')
->leftJoin('bidder_remarks', 'bidder_remarks.vehicle_id', '=', 'vehicles.id')
->leftJoin('vehicle_pins', 'vehicle_pins.vehicle_id', '=', 'vehicles.id')
->leftJoin('translations', 'translations.vehicle_id', '=', 'vehicles.id')
->where(function ($query) {
$query->where('vehicles.approve', '=', 1)
->orWhereNull('vehicles.approve');
})
->orderBy('vehicles.site', 'ASC')
->orderBy('vehicles.auc_time', 'ASC')
->select([
'vehicles.*',
'vehicle_details.color as vehi_color',
'vehicle_details.grade as vehi_grade',
'vehicle_details.images',
'makes.model_name',
'vehicle_extras.id as extra_details_id',
'vehicle_extras.ic_remarks',
'vehicle_extras.bidder_adj',
'reasons.fob_issue',
'reasons.fob_approve',
'reasons.decline_remark',
'reasons.tc_issue',
'reasons.fob_overruling_remark',
'bidder_invoices.invoice_name',
'bidding_views.reason as bidder_reason',
'bidder_prs.bidder_mb as bidder_mb',
'bidder_remarks.bidder_remarks as bidder_remarks_daily',
'revised_vehicles.old_mb_round',
'translations.assigned_translator'
])
->paginate(10000);
I have also added indexes for every join column. Also I have used MySQL as my DB
you can use the Eloquent relationships as methods in your Eloquent model classes refer Eloquent Relationship
if the relation is 1:1 use hasOne
if the relationship is 1:M use has Many
if the relationship is M:1 use belongs to
in your Vehicle model
for example
for 1:1
public function vehicleDetails() {
return $this->hasOne(VehicleDetail::class,'vehi_details_id','id');
}
for 1:M
public function VehicleExtras() {
return $this->hasMany(VehicleExtras::class,'id','vehicle_id');
}
for M:1
public function VehicleMakes() {
return $this->belongsTo(VehicleMakes::class,'model_id','model_id');
}
and after making all the relationship you can get the vehicle table with your filters as follows
$data['vehicles'] = Vehicles::whereNull('vehicles.deleted_at')
->where(function ($query) {
$query->where('vehicles.multiple_po', 0)
->orWhereNull('vehicles.multiple_po');
})
->where(function ($query) {
$query->where('vehicles.approve', '=', 1)
->orWhereNull('vehicles.approve');
})
->orderBy('vehicles.site', 'ASC')
->orderBy('vehicles.auc_time', 'ASC')
->paginate(10000);
then,
you can the vehicles in the blade as normal and if you needed to call the relationship table belongs to a certain id,
to get a description from vehicle details you can access as follows
$data['vehicles']->vehicleDetails->description
or to get the of a make of the vehicle from the makes table
$data['vehicles']->VehicleMakes->name

Laravel eloquent relationships Multiple table

When I try to connect my table using a query. But I need the building relationship using Laravel eloquent.
This is my query
{
$students = Student::with('FeeScheem')
->leftJoin('fee_scheems', 'fee_scheems.fs_id', '=', 'students.stu_fee_scheem_id')
->leftJoin('individually_fee_scheems', 'individually_fee_scheems.id', '=', 'students.stu_ind_fee_scheem_id')
->leftJoin('fee_masters', 'fee_masters.fee_master_ind_fee_scheem_id', '=', 'individually_fee_scheems.id')
->leftJoin('fee_groups', 'fee_groups.fg_id', '=', 'fee_masters.fee_master_fg_id')
->leftJoin('school_fee_collectors', 'school_fee_collectors.fc_fg_id', '=', 'fee_groups.fg_id')
->where('students.stu_id', $students)
->get();
$this->students = $students;
}

Pluck the values from WITH Eloquent in Laravel

I have a dropdown box that shows only the user with collector and borrower's role. Here's my code
public function create()
{
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');
})
->with('profile')
->get()
->pluck('name','id');
return view('dashboard.documents.create', compact('users'));
}
this code is fine but what I what to pluck is the column first_name, last_name and user_id from 'profile'.
How can I achieve that?
Thank you so much in advance!
You do one of these
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');})
->with('profile')
->select('name','id')
->get();
Or
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');})
->with('profile')
->get(['name','id']);
Both ways will return a collection with related models each model record will has the selected columns, if you want to convert it to an array you can add ->toArray() after the two ways and it will convert the collection to array.

How to join two queries as one in whereIn

In my Laravel-5.8 I have these queries:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', $employee_with_goals)
->get();
$employee_with_goals = DB::table('hr_employees')
->join('appraisal_goals', 'hr_employees.employee_code', '=', 'appraisal_goals.employee_code')
->select('hr_employees.employee_code')
->where('hr_employees.company_id', $userCompany)
->where('hr_employees.hr_status',0)
->where('appraisal_goals.is_published', '=', '1')
->where('appraisal_goals.is_approved', '=', '3')
->groupBy('hr_employees.employee_code')
->get();
$manager is the main query
How do I combine these two queries as one using $employee_with_goals as the variable in whereIn()?
You can use the pluck method to retrieve all values for a given key in a collection:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', $employee_with_goals->pluck('employee_code'))
->get();
You can actually use a subquery too:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', function ($query) use ($userCompany) {
$query
->table('hr_employees')
->select('hr_employees.employee_code')
->join('appraisal_goals', 'hr_employees.employee_code', '=', 'appraisal_goals.employee_code')
->where('hr_employees.company_id', $userCompany)
->where('hr_employees.hr_status',0)
->where('appraisal_goals.is_published', '=', '1')
->where('appraisal_goals.is_approved', '=', '3')
->groupBy('hr_employees.employee_code')
})
->get();
The code above is untested.

laravel eloquent union different column

I want join this table, but the columns is different.
$user_plays_aas = DB::table('user_plays')
->leftJoin('user_plays_aas', 'user_plays.id', '=', 'user_plays_aas.user_play_id')
->leftJoin('aas_games', function($join) {
$join->on('user_plays_aas.game_id', '=', 'aas_games.game_id')
->on('user_plays_aas.prd_id', '=', 'aas_games.prd_id');
})
->where('user_plays_aas.txn_id', $gameid);
$user_plays_pp = DB::table('user_plays')
->leftJoin('user_plays_pp', 'user_plays.id', '=', 'user_plays_pp.user_play_id')
->leftJoin('pp_games', 'user_plays_pp.gameID', '=', 'pp_games.gameID')
->where('user_plays_pp.roundid', $gameid);
$Union= $user_plays_aas
->unionAll($user_plays_pp );
How i make same columns $user_plays_aas and $user_plays_pp ? Cause Diff1 or Diff2 read as columns not as data, so make error.
$user_plays_aas->select('Same1','Same2', 'Diff1 as PP1', 'AAS1');
$user_plays_pp ->select('Same1','Same2', 'PP1', 'Diff2 as AAS1');
Thanks

Resources