Laravel eloquent relationships Multiple table - laravel

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;
}

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

how laravel join three tables with advance join clause using variable

Larvel have a question about inner joins with multiple tables and on values. I did build my code like this in laravel.
$clients = clients::leftjoin('clients_payment_type','clients_topup', function($join) {
$join->on('clients_payment_type.user_id', '=', 'clients.id') AND
$join->on('clients_topup.user_id', '=', 'clients.id');
})->where('clients.reference_id','=',$reference_id)->get();
Try this :
$clients = clients::leftjoin('clients_payment_type','clients_topup')
->join('clients','clients_payment_type.user_id', '=', 'clients.id')
->join('client_toup','clients.id', '=', 'clients_topup.user_id')
->where('clients.reference_id','=',$reference_id)
->get();
Regards
Working Answer
$clients1 = DB::table('clients')
->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
->where('clients.reference_id','=',$reference_id)
->get();

Laravel distinct on join results not working in query builder

I have a posts table that has join query with 4 other tables. 3 of them are one to one relations but the 4th is one to many. I want the query to return only 1 row for each post. What i am trying so far is like this-
$query = DB::table('posts')
->select('posts.*',
'subcategories.subcategory_title_en',
'subcategories.subcategory_title_bn',
'categories.category_title_en',
'categories.category_title_bn',
'users.*',
'postimages.postimage_thumbnail'
)
->join('subcategories', 'subcategories.subcategory_id', '=', 'posts.subcategory_id')
->join('categories', 'categories.category_id', '=', 'subcategories.parent_category_id')
->join('users', 'users.id', '=', 'posts.user_id')
->join('postimages', 'postimages.post_id', '=', 'posts.post_id');
$query->groupBy('posts.post_id');
echo $query->count();
exit();
I have currently 50 posts in database, but the query returns all rows for each postimages, more than 1 row for each post that is. I thought distinct would only show each post_id once? What am i missing here?
I would prefer if someone tells me how to do this with query builder. As this will have a lot of searching on different columns and i want it to be as fast as possible.
This is a simpler version -
$query = DB::table('posts')
->select('posts.*','postimages.postimage_thumbnail')
->join('postimages', 'postimages.post_id', '=', 'posts.post_id')
->groupBy('posts.post_id');
echo $query->count();
exit();
The strange thing is the SQL query that is shown by laravel " select posts.*, postimages.postimage_thumbnail from posts inner join postimages on postimages.post_id = posts.post_id group by posts.post_id" works fine in mysql
You should use groupby
Try this code
$query = DB::table('posts')
->select('posts.*',
'subcategories.subcategory_title_en',
'subcategories.subcategory_title_bn',
'categories.category_title_en',
'categories.category_title_bn',
'users.*',
'postimages.postimage_thumbnail'
)
->join('subcategories', 'subcategories.subcategory_id', '=', 'posts.subcategory_id')
->join('categories', 'categories.category_id', '=', 'subcategories.parent_category_id')
->join('users', 'users.id', '=', 'posts.user_id')
->join('postimages', 'postimages.post_id', '=', 'posts.post_id')->groupBy('posts.post_id');
In config/database.php at "mysql" change : 'strict' => true, to false

four tables joins in laravel eloquent

this query is for codeigniter how to make it in laravel eloquent
$this->db->select('*');
$this->db->from('countries cun');
$this->db->join('cities cit', 'cit.country_code=cun.country_code', 'left');
$this->db->join('city_states cits', 'cits.country_code=cun.country_code', 'left');
$this->db->join('timezone timz', 'timz.country_code=cun.country_code', 'left');
$this->db->where('cun.country',$id);
return $query = $this->db->get()->result();
Try this
return DB::table('countries AS cun')
->leftJoin('cities AS cit', 'cit.country_code', '=', 'cun.country_code')
->leftJoin('city_states AS cits', 'cits.country_code', '=', 'cun.country_code')
->leftJoin('timezone AS timz', 'timz.country_code', '=', 'cun.country_code')
->where('cun.country', $id)
->get();
Eloquent Model
return Country::leftJoin('cities AS cit', 'cit.country_code', '=', 'countries.country_code')
->leftJoin('city_states AS cits', 'cits.country_code', '=', 'countries.country_code')
->leftJoin('timezone AS timz', 'timz.country_code', '=', 'countries.country_code')
->where('countries.country', $id)
->get();
See here
Countries::with(["cities", "city_states", "timezone"])->where("country", $id)->get();
Countries - Eloquent Model.
cities, city_states, timezone - relationships in the Countries model.

groupby working in 3 tables and i need only one table

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();
}

Resources