Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

You could do:

public function SubSpeciality(){
    return SubSpeciality::where("SubSpecialityId", "=", $this->SubSpecialityId)->where("SpecialityId", "=", $this->SpecialityId)->first();
}

And then use:

Turn::find(1)->SubSpeciality()->name;

It should be more efficient than relations.

Last updated 2 years ago.
0

Awesome, but.. what if i have a long list of "turns" on a foreach that print the SubSpeciality()? There are a lot of querys in this way

Last updated 2 years ago.
0

You need to swap the keys on the belongsToMany relation definition, but in fact, this is not the relation you need here. If you have the foreign key on Turn model/table, then it's belongsTo relation:

public function SubSpeciality()
{
  return $this->belongsTo('Major\SubSpeciality', 'SubSpecialityID', 'SubSpecialityID');
}

If that's not the case, then describe your tables precisely.

Last updated 2 years ago.
0

If you can, add turnid column in subspeciality table.

For example you can get all subspecialty from a list of turn.ID simply doing:

$subspecialities = SubSpeciality::whereIn("turnid", $arrayOfTurnId)->get()->groupBy('turnid'); 

And get a subspecialty with a specific id using:

$subspcialities[$turnid]->name;

Some reason to avoid composite primary keys (framework or ORM are irrelevant): http://stackoverflow.com/questions/14112839/why-are-composite-keys-discouraged-in-hibernate

Last updated 2 years ago.
0

longilineo said:

If you can, add turnid column in subspeciality table.

For example you can get all subspecialty from a list of turn.ID simply doing:

$subspecialities = SubSpeciality::whereIn("turnid", $arrayOfTurnId)->get()->groupBy('turnid');

And get a subspecialty with a specific id using:

$subspcialities[$turnid]->name;

Some reason to avoid composite primary keys (framework or ORM are irrelevant): http://stackoverflow.com/questions/14112839/why-are-composite-keys-discouraged-in-hibernate

the db its from a thirty party developer and i can`t modify them :(

Last updated 2 years ago.
0

To be efficient you can use raw query

DB::raw("select ....")
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

cheycron cheycron Joined 12 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.