Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rocky's avatar
Level 1

Pass variable from controller to Model / hasMany OR / orderBy

Hello,

i've got this query in the controller:

$card = Card::with('CardToEdition', 'StageToCard', 'AttackToCard', 'FeatureToCard', 'FeatureToCard.FeatureTypeToCard', 'Type1ToCard', 'Type2ToCard', 'WeaknessType1ToCard', 'WeaknessType2ToCard', 'ResistanceType1ToCard', 'ResistanceType2ToCard','RarityToCard', 'TrainerTypeToCard', 'EnergyTypeToCard', 'ValuesToCard', 'SimilarToCard')
        ->where('setid', $editionid)
        ->where('setnumber', $setnumber)
        ->get();

Now i need to pass the $editionid to a model function to get further

what i want to have is

public function SimilarToCard($editionid) {
        return $this->hasMany('App\Models\TCG\Card', 'name_en', 'name_en')->where('setid', '!=', $editionid)->orderBy('id', 'desc')->take(5);
    }

Second thing is that i need in the has many to check for name_en and name_de because not every item hast both filled in the database. i want something like hasMany(Model, name_de, name_de, name_en, name_en)

Third thing is that i need to orderBy another table name not the given in the model. Actual im ordering by "card.id" but i want to order by "card_edition.date_released"

Thank you for your help

0 likes
0 replies

Please or to participate in this conversation.