Laravel + Eloquent : combine 2 tables in one query - php

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

Related

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 retrieve model with best match query

I have three tables seekers, jobs and skills. a user has many skills and also a job has many skills so i have these relations,
class Job extends Model
{
public function skills()
{
return $this->belongsToMany(Skill::class, 'jobs_skills', 'job_id', 'skill_id');
}
}
and
class Seeker extends Model
{
public function skills()
{
return $this->belongsToMany(Skill::class, 'seekers_skills', 'seeker_id', 'skill_id');
}
}
and
class skills extends Model
{
public function jobs()
{
return $this->belongsToMany(Job::class, 'jobs_skills', 'skill_id', 'job_id')->withPivot('level1');
}
public function seekers()
{
return $this->belongsToMany(Seeker::class, 'seekers_skills', 'skill_id', 'seeker_id');
}
}
I wrote this query to get the related jobs to the seekers based on skills
public function relatedJobs()
{
$skills = $this->belongsToMany(Skill::class, 'seekers_skills', 'seeker_id', 'skill_id')->pluck('skill_id');
$job_ids = DB::table('jobs_skills')->whereIn('skill_id', $skills)->pluck('job_id');
$jobs = Job::whereIn('id', $job_ids)->with('skills')->get()->sortBy(function ($job) {
return $job->skills2->groupBy('pivot.job_id')->count();
});
return $jobs;
}
This function return the related jobs but not sorted with the best matched with the skills of the seeker
any help ?
btw I need the return to be an object of Job

Eloquent model relationship for intermediate table

Consider the following table structure:
user table
id
name
lang_region_id
lang_region table
id
lang_id
region_id
lang table
id
name
region table
id
name
Fairly new to the Laravel framework, but trying to setup Eloquent models and relationships to an existing database. I want to establish the relationship between my user model and the lang and region models. The lang_region table defines what language and region combinations are available and then we can link each user to a valid combination.
I have read through the Laravel documentation several times looking for the proper relationship type, but is seems that the Many to Many and Has Many Through relationships are close, but since our user.id isn't used in the intermediate table I may be out of luck.
Sorry for the amateur question, but just getting used to Laravel and ORMs in general.
I would use the lang_region table as both a pivot table and a regular table with its own model.
class LangRegion extends model
{
protected $table = 'lang_region';
public function language()
{
return $this->belongsTo(Language::class, 'lang_id');
}
public function region()
{
return $this->belongsTo(Region::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
class User extends model
{
protected $table = 'user';
public function langRegion()
{
return $this->belongsTo(LangRegion::class);
}
}
class Language extends model
{
protected $table = 'lang';
public function regions()
{
$this->belongsToMany(Region::class, 'lang_region', 'lang_id', 'region_id');
}
public function users()
{
$this->hasManyThrough(User::class, LangRegion::class, 'lang_id', 'lang_region_id');
}
}
class Region extends model
{
protected $table = 'region';
public function languages()
{
$this->belongsToMany(Language::class, 'lang_region', 'region_id', 'lang_id');
}
public function users()
{
$this->hasManyThrough(User::class, LangRegion::class, 'region_id', 'lang_region_id');
}
}
If I understand what you want correctly:
class User extends Model {
private function lang_region() {
return $this->hasOne(LangRegion::class)
}
public function lang() {
return $this->lang_region()->lang();
}
public function region() {
return $this->lang_region()->region();
}
}
class LangRegion extends Model {
public function lang() {
return $this->belongsTo(Lang::class);
}
public function region() {
return $this->belongsTo(Region::class);
}
}

Laravel Eloquent Multiple Join

I have a table albums, each album has multiple songs, artworks, and can belong to a number of series.
Each of the songs can have lyrics from another table.
So far I have :
routes.php
Route::get('get_albums', function() {
return Album::with('songs', 'artworks', 'series')->get();
});
Album.php model
<?php
class Album extends Eloquent {
protected $table = 'albums';
public function songs()
{
return $this->hasMany('Song');
}
public function artworks()
{
return $this->hasMany('Artwork');
}
public function series()
{
return $this->hasMany('Serie');
}
}
Song.php model
<?php
class Song extends Eloquent {
public function album()
{
return $this->belongsTo('Album');
}
public function lyric() {
return $this->hasOne('Lyric');
}
}
Artwork.php model
<?php
class Artwork extends Eloquent
{
public function album()
{
return $this->belongsTo('Album');
}
}
Serie.php model
<?php
class Serie extends Eloquent {
public function album()
{
return $this->belongsTo('Album');
}
}
Lyric.php model
<?php
class Lyric extends Eloquent {
public function song()
{
return $this->belongsTo('Song');
}
}
This gives me back all the albums with their songs, artworks, and series. Trying to figure out how to do the 2nd join to get the lyrics for the songs.
You can try
Route::get('get_albums', function() {
return Album::with('songs.lyric', 'artworks', 'series')->get();
});

laravel returning relationships with eloquent and laravel

In my database I am an organisations table this table has the following relationships,
Many-to-many with users
Many-to-many with clients
One-to-many with projects
In turn these relationships have other relationships for example projects
One-to-one with client
In my controller I am doing the following,
$organisation = Organisation::all();
$organisation->load('users');
$organisation->load('clients');
$organisation->load('teams');
$organisation->load('projects');
return Response::json($organisation, 200);
So get all the organisations and there relational data.
However what I wanting to do is also get the relationships of relationships, so for example get the client that is related to each project that an organisation has? I thought that doing what I am doing would have worked but obviously not.
Here are my models,
Organisation,
class Organisation extends Eloquent {
//Organsiation __has_many__ users (members)
public function users()
{
return $this->belongsToMany('User')->withPivot('is_admin');
}
//Organisation __has_many__ clients
public function clients()
{
return $this->belongsToMany('Client');
}
//Organisation __has_many__ projects
public function projects()
{
return $this->belongsToMany('Project');
}
}
Projects
class Project extends Eloquent {
protected $fillable = [
'name',
'description',
'total_cost',
'start_date',
'finish_date',
'sales_person',
'project_manager',
'client_id',
'organisation_id',
'user_id'
];
public function organisations()
{
return $this->belongsToMany('Organisation');
}
public function salesperson() {
return $this->belongsTo('User', 'sales_person');
}
public function clients() {
return $this->belongsTo('Client', 'client_id');
}
}
Clients
class Client extends Eloquent {
public function organisations()
{
return $this->belongsToMany('Organisation');
}
public function users()
{
return $this->belongsToMany('User');
}
public function projects()
{
return $this->hasMany('Project');
}
}
Have you tried:
$organisations = Organisation::with('projects', 'projects.clients')->all();
foreach($organisations as $organisation) {
foreach($organisation->projects as $project) {
foreach($project->clients as $client) {
echo $client->name;
}
}
}

Categories