Group by and Sum of One-To-Many relation tables in Eloquent - mysql

I have a requirement.
My DB has tables like the following.
The tables have OneToMany (1-n) parent-child relation.
Table School (id, school_name)
Table Class (id, school_id, class_name)
Table Section (id, class_id, section_name, no_of_seats)
Table Student (id, section_id, student_name, ....)
When Some Student is registered, data is uploaded to the Student table.
Now, I want to have a statistic like
| school_name | total_seats | student_registered |
and for a particular school
| class_name | total_seats | student_registered |
How to achieve this in Laravel/Eloquent
Thanks in Advance

Probably it works with:
Counting/Summarizing HasMany relations
Counting/Summarizing HasManyThrough relations
Counting/Summarizing HasManyDeep relations
Definition
class Section extends Model
{
public function students(): HasMany
{
return $this->hasMany(Student::class);
}
public function scopeWithRegisteredStudents(Builder $query): Builder
{
// Count HasMany relation
return $query->withCount('students as students_registered');
}
}
// The word "Class" is reserved, so we need to use "SchoolClass" instead
class SchoolClass extends Model
{
protected $table = 'classes';
public function sections(): HasMany
{
return $this->hasMany(Section::class, 'class_id');
}
public function students(): HasManyThrough
{
return $this->hasManyThrough(Student::class, Section::class, 'class_id');
}
public function scopeWithTotalSeats(Builder $query): Builder
{
// Summarize field from HasMany relation
return $query->withSum('sections as total_seats', 'no_of_seat');
}
public function scopeWithRegisteredStudents(Builder $query): Builder
{
// Count HasManyThrough relation
return $query->withCount('students as students_registered');
}
}
class School extends Model
{
public function classes(): HasMany
{
return $this->hasMany(SchoolClass::class);
}
public function sections(): HasMany
{
return $this->hasManyThrough(Section::class, SchoolClass::class, null, 'class_id');
}
public function students(): HasManyThrough
{
// https://github.com/staudenmeir/eloquent-has-many-deep
return $this->hasManyDeep(Student::class, [SchoolClass::class, Section::class], ['school_id', 'class_id', 'section_id'], ['id', 'id', 'id']);
}
public function scopeWithTotalSeats(Builder $query): Builder
{
// Summarize field from HasManyThrough relation
return $query->withSum('sections as total_seats', 'no_of_seat');
}
public function scopeWithRegisteredStudents(Builder $query): Builder
{
// Count HasManyDeep relation
return $query->withCount('students as students_registered');
}
}
Example
// Fetching simply
Section::query()
->withRegisteredStudents()
->get();
SchoolClass::query()
->withTotalSeats()
->withRegisteredStudents()
->get();
School::query()
->withTotalSeats()
->withRegisteredStudents()
->get();
// Fetching with nested relations
School::query()
->withTotalSeats()
->withRegisteredStudents()
->with(['classes' => function (HasMany $query) {
return $query
->withTotalSeats()
->withRegisteredStudents();
}])
->get();
If you use a static analyzer like PHPStan or Psalm, you can alternatively use scopes method to prevent errors.
School::query()
->scopes(['withTotalSeats', 'withRegisteredStudents'])
->get();

This is not what you asked for as it uses Query Builder instead of Eloquent. I have not tested it as I have nothing to test against currently but this should work -
use Illuminate\Support\Facades\DB;
$students_per_section = DB:table('students')
->select('section_id', DB::raw('COUNT(id) AS num_students'))
->groupBy('section_id')
$query = DB:table('schools')
->join('classes', 'schools'.'id', '=', 'classes.school_id')
->join('sections', 'classes.id', '=', 'sections.class_id')
->leftJoinSub($students_per_section, 'students_per_section', function($join) {
$join->on('sections.id', '=', 'students_per_section.section_id')
});
if ($school_id) {
$query
->select('classes.class_name', DB::raw('SUM(no_of_seats) AS total_seats'), DB::raw('SUM(students_per_section.num_students) AS student_registered'))
->where('schools.id', '=', $school_id)
->groupBy('classes.class_name')
} else {
$query
->select('schools.school_name', DB::raw('SUM(no_of_seats) AS total_seats'), DB::raw('SUM(students_per_section.num_students) AS student_registered'))
->groupBy('schools.school_name')
}
$stats = $query->get();

Related

How to update a pivot table with multiple relations using Eloquent in laravel 8

I am working with laravel 8, it causes me the doubt how to update that data from user the pivot table
class User extends Authenticatable
{
public function roles()
{
return $this->belongsToMany(Role::class,'contracts');
}
public function offices()
{
return $this->belongsToMany(Office::class,'contracts');
}
public function documents(){
return $this->hasMany(Document::class,'creator_id');
}
}
There is a many-to-many relationship between roles and offices that provides me with a pivot table like so:
Table Name: contracts
Colums:
id
user_id
office_id
role_id.
And I tried this way but it only updates me the user data but in the pivot table it does nothing
public function update($id, UpdateUserRequest $request)
{
if (empty($id)) {
return $this->sendError('User not found');
}
$userUpdate = User::find($id);
$userUpdate->name = $request->name;
$userUpdate->document = $request->document;
$userUpdate->email = $request->email;
if ($request->password != null) {
$userUpdate->password = bcrypt($request->password);
} else {
$userUpdate->password = $userUpdate->password;
}
$userUpdate->save();
$userUpdate->offices()->updateExistingPivot($request->office_id,['role_id'=>$request- >role_id]);
return $this->sendResponse($userUpdate->toArray(), 'User updated successfully');
}
I need to solve that problem.

many-to-many relationship: order by on pivot table not working

I have these relationship between school and associate models:
// School model
public function associates()
{
return $this->belongsToMany('Associate', 'school_associate', 'school_id', 'associate_id')
->withPivot('start_date', 'end_date');
}
// Associate model
public function schools()
{
return $this->belongsToMany('School', 'school_associate', 'associate_id', 'school_id')
->withPivot('start_date', 'end_date');
}
I need to get all associates of one school ordered by start_date.
This is what I tried without success (in this try I am searching in all schools):
dd(\App\Associate::with(['schools' => function ($q) {
$q->orderBy('pivot_start_date', 'desc');
}])->toSql());
And I get this sql (notice no order by clause):
select * from `associate`
I tried to edit the relationship like this:
// Associate model
public function schools()
{
return $this->belongsToMany('School', 'school_associate', 'associate_id', 'school_id')
->withPivot('start_date', 'end_date')
->orderBy('pivot_start_date', 'desc'); // also tried without "pivot_"
}
And according to this post, I also tried :
// Associate model
public function schools()
{
return $this->belongsToMany('School', 'school_associate', 'associate_id', 'school_id')
->withPivot('start_date', 'end_date')
->orderBy('school_associate.start_date', 'desc');
}
But I always get the same query and the results are not ordered.
I solved using query builder in this way.
This function is in Associate model:
public function scopeLast($query, $school_ids = [])
{
$query->join('school_associate', "{$this->table}.{$this->primaryKey}", '=', 'school_associate.associate_id')
->join('school', 'school.school_id', '=', 'school_associate.school_id')
->whereIn('school.school_id', $school_ids)
->orderBy('school_associate.start_date', 'desc');
return $query;
}

Select records that haven't specific value as a child

I want to get users filtered by category, so I send category_id as a parameter and should select users who haven't records in users_categories_restrictions table with this category_id.
How can I make this eloquent query ??
You could use whereDoesntHave
$users = App\User::whereDoesntHave('categories', function ($query) use($cat_id) {
$query->where('categories.id', '=', $cat_id);
})->get();
I assume you have defined many to many relation between user and category model
class User extends Model
{
public function categories()
{
return $this->belongsToMany(Category::class, 'users_categories_restrictions', 'user_id');
}
}
class Category extends Model
{
public function users()
{
return $this->belongsToMany(User::class, 'users_categories_restrictions', 'category_id');
}
}

Laravel Autocomplete using foreign key to show data from another table

i have created an auto complete search box in controller of 'booking' table successfully , but i want the auto complete search box to show data from another table 'patient' that have a one to many relationship with "booking" table according to a specific condition using 'where' condition ,
This is the Booking Controller that i add autocomplete in it:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Booking;
use App\Patient;
use App\User;
use Session;
use DB;
use Auth;
use Input;
class BookingController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$search = \Request::get('search');
$bookings = Booking::whereHas('patient', function ($query) use ($search) {
$query->where('patient_name', 'like', '%' . $search . '%');
})->where('status','=', null)->whereHas('patient', function ($query){
$query->where('company_id','=' ,Auth::user()->company_id);
})->paginate(10);
return view('booking.index')->withBookings($bookings);
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function autoComplete(Request $request) {
$query = $request->get('term','');
$bookings=Booking::whereHas('patient', function ($query){
$query->where('company_id','=' ,Auth::user()->company_id);
})->$data=array();
foreach ($bookings as $booking) {
$data[]=array('value'=>$booking->patient->patient_name,'id'=>$booking->id);
}
if(count($data))
return $data;
else
return ['value'=>'No Result Found','id'=>''];
}
and this is the Booking Model :
class Booking extends Eloquent
{
public function patient()
{
return $this->belongsTo('App\Patient');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
and this is the patient Model:
class Patient extends Eloquent
{
public function booking()
{
return $this->hasMany('App\Booking');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
and i used this code in view :
{!! Form::text('search_text', null, array('placeholder' => 'Search Text','class' => 'form-control','id'=>'search_text')) !!}
i want to show data from "patient" table and there is a one to many relationship between "booking" and "patient" table and i have successfully made a search box to search in patient table as you can see in index function , but i dont know to show data from "patient" table using where condition to show patient_name that his company_id equal Authenticated user company_id
Sorry for my Bad Language .

Querying 'across' pivot table

I have two models, beers and distributions, which have a many-to-many relationship. The pivot model hasMany kegs, which contain some relevant information to the beer such as pricing and status. When I build my beer index, I need all the information of the beer model, the distributor model, and the keg model. What I am trying to figure out is how to query for all the information in an efficient manner. Here is my current query:
Keg's are scoped on status:
public function scopeStatus($query, $status)
{
return $query->where('status', '=', $status);
}
and I build my beers index with:
$kegs = Keg::status($status)->get();
$beers=[];
foreach ($kegs as $keg){
$beer = Beer::find($keg->beer_distribution->beer_id);
$distributor = Distributor::find($keg->beer_distribution->distributor_id);
$beers[]=[
'beer' => $beer,
'keg' => $keg,
'distributor' => $distributor];
}
return $beers;
I know that this is a slow query but im not sure how to do this in a single query. Is there a way that I can run this faster?
Some relevant model code:
class Beer extends Eloquent {
public function distributors()
{
return $this->belongsToMany('Distributor', 'beer_distributions');
}
class BeerDistribution extends Eloquent {
protected $fillable = ['beer_id', 'distributor_id'];
public function kegs()
{
return $this->hasMany('Keg', 'beer_distribution_id');
}
class Distributor extends Eloquent {
public function beers()
{
return $this->belongsToMany('Beer', 'beer_distributions');
}
class Keg extends Eloquent {
public function scopeStatus($query, $status)
{
return $query->where('status', '=', $status);
}
public function beerDistribution()
{
return $this->belongsTo('BeerDistribution');
}
}
So I figured out that what I really needed to do was add my query building relations on my Keg model (which was the fatherest 'down' in the nest of relations), and then use eager loading!
I now build my beers index like so:
$beers=[];
foreach (Keg::status($status)
->with('kegsize',
'beerDistribution.beer.brewery',
'beerDistribution.beer.style',
'beerDistribution.distributor')->get() as $keg){
$beers[]=$keg;
}
return $beers;
This brings me down to a stunning total of 10 queries.