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.
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
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.
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
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 :(
To be efficient you can use raw query
DB::raw("select ....")
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community