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();
});
Related
I have three models Driver Worker and Group. The models are like this:
class Group extends Model
{
.....
public function driver()
{
return $this->belongsTo('App\Driver');
}
public function worker()
{
return $this->belongsTo('App\Worker');
}
}
Drivers
class Driver extends Model
{
public function groups()
{
return $this->hasMany('App\Group');
}
}
Worker
class Workerextends Model
{
public function groups()
{
return $this->hasMany('App\Group');
}
}
But how can I get a single element with the relationships that I have defined in my models. I have this in my controller but I only get the first element of the table. I need to get the element with the id and also the relations.
public function show(Group $group)
{
return $group->with(['worker','driver'])->first();
}
try this
public function show(Group $group,$id){
$group = $group->where('id',$id)->with(['worker','driver'])->first();
return $group
}
in your Route
Route::get('/group/show/{id}', 'GroupController#show')->name('group.show');
I've following relationship:
class Project extends Model
{
public function invitors()
{
return $this->hasMany(Invitation::class)
}
}
// User model
class User extends Model
{
public function invitations()
{
return $this->morphMany('App\Invitation', 'invitee');
}
}
// Business Model
class Business extends Model
{
public function invitations()
{
return $this->morphMany('App\Invitation', 'invitee');
}
}
// Invitation model
class Invitation extends Model
{
public function invitee()
{
return $this->morphTo();
}
public function project()
{
return $this->belongsTo(Project::class);
}
}
What I want is to get project invitors (users, and businesses). When I use from $project->invitors->invitee its not working.
In your example $project->invitors is a collection of Invitation instances, and it hasn't property invitee. You can use helper pluck to loop $project->invitors and get their invitees:
$invitees = $project->invitors->pluck('invitee');
As a response to the first comment:
to get specific attributes of fetched linked models ("invitee") you can specify accessors:
class Business extends Model
{
...
public function getInviteeNameAttribute() {
return $this->name;
}
}
class User extends Model
{
...
public function getInviteeNameAttribute() {
return $this->first_name . ' ' . $this->last_name;
}
}
...then get that attribute from invitee:
$invitees[0]->invitee_name
See https://laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor
I've read many posts about this issue but none of them works for me. I have a 'ISA' relationship in my database. A person can be either a Patient or a Nurse:
class Person extends Model
{
protected $table = 'persons';
public function commentable()
{
return $this->morphTo();
}
}
class Patient extends Model
{
public function persons()
{
return $this->morphMany('App\Person', 'commentable');
}
}
class Nurse extends Model
{
public function persons()
{
return $this->morphMany('App\Person', 'commentable');
}
}
This is my tables and the data inside them:
And this is my Route:
Route::get('person', function () {
$person = Person::find(1)->commentable();
return json_decode(json_encode($person), true);
});
I get an empty array!
You have to access the relationship as a property:
$person = Person::find(1)->commentable;
I have following eloquent.
Volume,
Issue,
Category,
Article,
ArticleTranslation,
Volume can have many issues.
Issue can have many categories.
Category can have many articles.
Article can have many translation.
SO how can i get Volume from Article/ArticleTranslation?
First your models.
..
class Volume extends Model {
public function issues() {
return $this->hasMany(Issue::class);
}
}
class Issue extends Model {
public function volume() {
return $this->belongsTo(Volume::class);
}
public function categories() {
return $this->hasMany(Category::class);
}
}
class Category extends Model {
public function issue() {
return $this->belongsTo(Issue::class);
}
public function articles() {
return $this->hasMany(Article::class);
}
public function articlesTranslated() {
return $this->hasMany(ArticleTranslated::class);
}
}
class Article extends Model {
public function category() {
return $this->belongsTo(Category::class);
}
}
..
Then in your code:
..
$articles = Articles::all();
$volumes = [];
foreach ($articles as $article) {
$volumes[] = $article->category->issue->volume;
}
..
Docs
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');
}
}