Eloquent Eager Load Double Relationship - laravel

This one's been bothering me and Google hasn't been of any help. I have two models, a Dance model and an Author model. The relationship is that an author can have many dances. The wrinkle is that there are actually two relationships between these objects, as there is the original author and an author much later credited with reconstructing the dance (these are historical dances, after all). I can eager load with the load() function for the relation that uses the normal foreign key, but I cannot find out how to eager load the relation that uses a differently-named foreign key.
How do I do eager loading on this second relation?

When setting up a relationship with a different foreign key you have to specify in the relationship which key you wanna use...return $this->hasOne('Author', 'foreign-key');
So if I understand your question you wanna setup a relationship to two authors, in the same author table....
So maybe something like this then?? (replace foreign_key with your table field)
public function orig_author(){
return $this->hasOne('Author');
}
public function second_author(){
return $this->hasOne('Author', 'foreign-key');
}
public function dance(){
return $this->hasMany('Dance');
}
Then just retrieve it like so...($id being the id of your dance)
$dance = Dance::with('orig_author','second_author')->find($id);
But to be honest, I've never tried having a double relationship with a table.....let me know if it works out for you. :) Id be very interested.

You can do this:
$dance = ModelName::query()
->with('eagerNameOne')
->with('eagerNameTwo')
->find($id);
where ::query() was added only tu put all the eagers on the same identation level.

Related

Nested eloquent relationship

I just started my first laravel project today and stumbled upon this confusing situation,
I intended to use eloquent relationship instead of manually joining tables on query. So here it goes...
I have 3 tables which are users , tbl_instruments_taught , tbl_instruments
users table does not hold any of the table's ID,
tbl_instruments_taught - holds the id of user and intruments
Note: user can teach multiple instruments
Now, I implemented a hasOne on my other result set but I dont think hasOne works on this one. I read about belongsToMany but as I try implement it, it seems like every minute, it gets confusing.
Any idea?
I think you are looking for pivot table and relation many to many.
You have users table, instruments table (user has many instruments and one instrument could belong to many users) and user_instruments pivot table. Here db model:
And then add belongsToMany relationship in User and Instrument model (with specified pivot table). Like so:
//IN USER MODEL
public function instruments()
{
return $this->belongsToMany('App\Models\Instruments', 'users_instruments', 'user_id', 'instrument_id');
}
//IN INSTRUMENT MODEL
public function users()
{
return $this->belongsToMany('App\Models\Users', 'users_instruments', 'insrument_id', 'user_id');
}
Now you can reach instruments/users data through laravel relations/eager loading.

laravel 3 way pivot

I've one many to many relationship between Entities and Affiliated Tables,
basically an affiliated can attend to many entities(example, medical services, dentist offices). One Entity can be visited by one or many affiliated.
Additionaly , one or more benefits can be occupied by one affiliated in a particular entity. As shown in the image.
I want to be able to answer queries like, which benefits where used by the affiliated in a particular entity.
Should i create a new Model, how should i rewrite the many-to-many relationship for ORM.
Thanks in advance!
Don't need a Affiliated_has_Entities model.
You could write the relationships as below, remember to check for arguments and its order in the belognsToMany relationship (sorry for misspelled words)
Entities model
public function afilliateds(){
return $this->belongsToMany('Afilliated', 'Afilliated_has_Entities');
}
Afilliated model
public function entities(){
return $this->belongsToMany('Entities', 'Afilliated_has_Entities');
}
public function benefits(){
return $this->belongsToMany('Entities', 'Afilliated_has_Entities');
}
Benefits model
public function entites(){
return $this->belongsToMany('Entities', 'Afilliated_has_Entities');
}
And so on..
I hope it helps

how to load foreign relationship laravel 5

I am storing information on a 4-day class. The information is dates 1-4 and each date has an instructor associated with it as there can be different instructors on different days. Let's just talk about day_1 and instructor_day_1 for now. My models are as follows:
Course Model:
public function instructor()
{
return $this->belongsTo('App\Instructor');
}
Instructor Model:
public function course()
{
return $this->hasMany('App\Course');
}
When i go to the #show method in my controller, i find the correct course but when i try to load the view, i can't figure out how to access the foreign relation. Right now i'm trying:
<p class="card-text">{{$course->day_1}} assigned to {{$course->instructor_day_1->instructor->name}}</p>
But that's not yielding anything but errors. If i do {{$course->instructor_day_1}}, i get the correct value from the database.
Is my relationship backwards? Does Course model need be "belongsToMany"? What is the correct syntax for pulling the instructor info? Do i need to specify any foreign key relations in my models?
belongsTo is the correct relationship, but it's not set up correctly. And you'll need 4 of them, 1 for each foreign key on the courses table.
This should get you started:
public function instructorDay1()
{
return $this->belongsTo('App\Instructor', 'instructor_day_1');
}
And then you would call it like this, to get instructor's name:
$course->instructorDay1->name;
And you'll need to do this for all 4 relationships.

eloquent filter result based on foreign table attribute

I'm using laravel and eloquent.
Actually I have problems filtering results from a table based on conditions on another table's attributes.
I have 3 tables:
venue
city
here are the relationships:
a city has many locations and a location belongs to a city.
a location belongs to a venue and a venue has one location.
I have a city_id attribute on locations table, which you may figured out from relationships.
The question is simple:
how can I get those venues which belong to a specific city?
the eloquent query I expect looks like this:
$venues=Venue::with('location')->where('location.city_id',$city->getKey());
Of course that's not gonna work, but seems like this is common task and there would be an eloquent command for it.
Thanks!
A couple of options:
$venues = Venue::whereIn('location_id', Location::whereCityId($city->id)->get->lists('id'))
->get();
Or possibly using whereHas:
$venues = Venue::whereHas('location', function($query) use ($city) {
$query->whereCityId($city->id);
})->get();
It is important to remember that each eloquent query returns a collection, and hence you can use "collection methods" on the result. So as said in other answers, you need a Eager Loading which you ask for the attribute you want to sort on from another table based on your relationship and then on the result, which is a collection, you either use "sortBy" or "sortByDesc" methods.
You can look at an example below:
class Post extends Model {
// imagine timpestamp table: id, publish, delete,
// timestampable_id, timestampble_type
public function timestamp()
{
return $this->morphOne(Timestamp::class, 'timestampable');
}
}
and then in the view side of the stuff:
$posts = App\Post::with('timestamp')->get(); // we make Eager Loading
$posts = $posts->sortByDesc('timestamp.publish');
return view('blog.index', compact('posts'));

Laravel / Eloquent - custom relation method

I have a class Report which has a belongsToMany relation to Metric. Report also additionally has a belongsTo relation to Metric.
Normally, the model returned by the belongsTo relation is the same as one of the models in the belongsToMany relation. When this is true I'd like it to be the case that each of the two relations actually looks at the same object instance (this also saves an extra trip to the db).
So, in basic terms - is there a way to get one relation to check another first, to see if a model has already been loaded, and if so, point to that object rather than creating a new one.
I tried putting some code in the belongsTo relation method for Metric but I can't get round the fact it needs to return an instance of belongsTo, which needs various things passed as constructor arguments (ie. a query object), which aren't relevant in that case that the model has already been loaded in the belongsToMany relation.
I thought of ditching the belongsTo relation and adding data horizontally in the pivot table for the belongsToMany relation, but it isn't a many-to-many relation required so that seems a bit wrong.
Thanks!
Geoff
The idea here is to write a function which would check if a relationship is loaded and return that relationship, otherwise it will return the belongsToMany. This would go in your Report class. This is also for Laravel 5. If you have 4, just remove the namespaces from the model names.
public function metric()
{
return $this->belongsTo('App\Metric');
}
public function metrics()
{
return $this->belongsToMany('App\Metric');
}
public function getMetric()
{
if(array_key_exists('metric', $this->getRelations())) {
return $this->metric;
}
return $this->metrics()->first();
}
If you do decide to just go with a belongsToMany only, I'd suggest putting a unique key on your pivot table for both ID's to keep from getting any duplicates in the pivot table.

Resources