laravel 5 get data from 3 tables with group by - php

I have 3 tables schools, classes, attendances which are
schools
public function classes() {
return $this->hasMany('App\Models\Classes');
}
classes
public function school() {
return $this->belongsTo('App\Models\School');
}
public function attendances() {
return $this->hasMany('App\Models\Attendance');
}
attendances
public function classes() {
return $this->belongsTo('App\Models\Classes');
}
Now I want to get data of attendances group by date which consist of classes for each school, like following
date1{
total attendance: totalNumber,
classes{
class1{},
class2{},
}
},
date2{
....
}

Related

laravel recursive relationship with directed acyclic graph

i have table "task" and relationship many to many with itself
public function depends_on() //parents
{
return $this->hasMany(DependentTask::class, 'task_id');
}
public function dependents() //kids
{
return $this->hasMany(DependentTask::class, 'dependent_id');
}
the relationship may be like this
i want to select the tasks like this
first print 5 or 2
then 3 and 6
then 4 and 1
how can i do it by laravel?
This is how you can use recursive relations:
public function dependsOn()
{
return $this->hasMany('Account', 'task_id','dependent_id');
}
public function dependents()
{
return $this->dependsOn()->with('dependents');
}
Then:
$dependents = Dependents::with('dependents')->first();
$dependents->dependents;
$dependents->dependents->first()->dependents; // .. and so on

laravel relationship between 3 tables - many-to-many

I have 3 tables (Models)
Advertise :
id
title
...
Value:
id
title
amount
....
Field:
id
name
title
...
and my pivot tables is like:
advertise_id
value_id
field_id
how can I set relations in my models and do a crud(please give me an example)?
all the relations are many to many
3 model classes accordingly:
class Advertise extends Model
{
public function fields()
{
return $this->belongsToMany(Field::class);
}
public function values()
{
return $this->belongsToMany(Value::class);
}
}
class Field extends Model
{
public function advertises()
{
return $this->belongsToMany(Advertise::class);
}
public function values()
{
return $this->belongsToMany(Value::class);
}
}
class Value extends Model
{
public function fields()
{
return $this->belongsToMany(Field::class);
}
public function advertises()
{
return $this->belongsToMany(Advertise::class);
}
}

Tracking history of vehicles and drivers

I'm experimenting with relations in Laravel and I cannot figure this one out.
I have two tables users and vehicles. The users can drive a vehicle and the drivers are replaced often. To track these changes I created a history table. This table contains user_id, vehicle_id, start and end.
I want to get the vehicle that the current user is driving, through the History table.
class User extends model {
public function vehicle()
{
// return the current active vehicle for the user
// through the History model.
return $this->hasOne(History::class, 'user_id');
}
public function history()
{
// return all the vehicles that the user has used
// this currently returns the history rows, not the cars
return $this->hasMany(History::class, 'user_id');
}
}
class Vehicle extends model {
public function user()
{
// return the current active user
}
public function users()
{
// return all the users that used this vehicle
}
}
If I perform the following method in a controller.
public function showUser()
{
return User::findOrFail(1)->with('vehicle', 'history')->get()
}
I want the response formatted like the following:
[
0: {
first_name: 'John',
...
vehicle: {
model: 'BMW ...'
}
history: {
0: {
model: 'BMW ...'
}
1: {
model: 'Audi ...'
}
...
}
}
]
You could add a query scope to your history model to return the latest record.
public function scopeLatest($query)
{
return $query->orderBy('created_at', 'desc')->limit(1);
}
Then you can get that through your relationship
$user->history()->latest()->first()->vehicle;
This, of course, requires that you have the vehicle relationship defined in your history class.

Laravel Eloquent model JOIN a MAX record

I'm new to Laravel and I'd like to know how to use eloquent model to join a max(date) record...
App\SchemasGroup::find(7)->schemas()->get()->max('schemasVersion')->get();
I find a group of schemas (schemas_groups table) based on ID, then get the schemas from that group (schemas table), and I want to join the 'date' and 'version' field from schemas_versions table with the last version (so, max date or version field) ...
Relations are defined as:
class SchemasGroup extends Model
{
public function schemas() { return $this->hasMany('App\Schema'); }
}
class Schema extends Model
{
public function group() { return $this->belongsTo('App\SchemasGroup'); }
public function versions() { return $this->hasMany('App\SchemasVersion'); }
}
class SchemasVersion extends Model
{
public function schema() { return $this->belongsTo('App\Schema'); }
public function updatedBy() { return $this->belongsTo('App\User','updated_by'); }
}
Getting the user name who updated that last version would also be lovely...
Apparently it was easy with defining chaining models.
class Schema extends Model
{
public function group() { return $this->belongsTo('App\SchemasGroup'); }
public function versions() { return $this->hasMany('App\SchemasVersion'); }
public function latestVersion() { return $this->hasOne('App\SchemasVersion')->latest(); }
}
and then fetching the data with:
App\SchemasGroup::with('schemas.latestVersion.updatedBy')->find($schemaGroupId);

Laravel + Eloquent : combine 2 tables in one query

I am trying to perform a query with Eloquent where I can get all the courses of an user.
Users have their courses because they are in some grade. So all courses of this grade will be included.
$user->grade->courses
Now I'd like to include some courses that are not in the user's grade thanks to the pivot table : users_courses. But I don't know how to query all tables at once using Eloquent.
EDIT : Here is what have
class Users extends Eloquent {
public function courses()
{
return $this->belongsToMany('Courses', 'users_courses', 'student_id', 'course_id');
}
public function grade()
{
return $this->belongsTo('Grades', 'grade_id');
}
}
class Courses extends Eloquent {
public function grade()
{
return $this->belongsTo('Grades');
}
public function users()
{
return $this->belongsToMany('Users', 'users_courses', 'student_id', 'course_id');
}
}
class Grades extends Eloquent {
public function courses()
{
return $this->hasMany('Courses', 'grade_id');
}
public function users()
{
return $this->hasMany('Users', 'grade_id');
}
}

Categories