0

I have a laravel object where I would like to get its relations. However, the key that I would use to bind the relation could be one of two.

For example, let say I have a fictional "wine pack" which is essentially a box of three bottles of wine - either three red, three white or three rose. The "Pack" is sold as one product, but the bottles of wine can also be sold individually.

Controller.php

$wine_pack = Pack::with('wine')->get();

Wine.php

class Pack
{
    public function wine()
    {
        return $this->belongsTo(Wine::class, 'wine_sku', 'sku');
    }
}

This code actually works. But in the real world, white wine is identified by its 'sku'. Red wine and Rose wine are identified by their 'ref_id'.

I am trying to find a way to look for the individual wine by their sku, and if none are found, look by their ref_id. For example:

class Pack
{
    public function wine()
    {
        $wine = $this->belongsTo(Wine::class, 'wine_sku', 'sku');

        if($wine->count() <= 0)
            $wine = $this->belongsTo(Wine::class, 'wine_ref_id', 'ref_id');

        return $wine;

    }
}

Is this possible?

Typhoon101
  • 2,063
  • 8
  • 32
  • 49
  • Is there a technical reason why you can't use a standard `id` column for all wines, and simply leave `sku` and `ref_id` as additional columns? In this case, `id` is just a technical implementation for the relational database, and the other details are properties of the Wine objects that can be displayed as identifiers for humans. – Aken Roberts Jul 01 '19 at 20:36
  • the 'wine' objects come from different suppliers. One uses a sku, one uses a ref_id. we don't have the ability to map either id into a common field. – Typhoon101 Jul 02 '19 at 12:27
  • I don't suggest mapping them to a common field. I suggest having the standard ID column (auto-increment, UUID, whatever) and use that when dealing with MySQL foreign keys and Eloquent relationships. Then, keep sku and ref_id as separate fields, and fill them in appropriately depending on the type of wine (which you could also store in the table for reference if it helps your app). – Aken Roberts Jul 02 '19 at 16:10
  • the database is already setup and would be a huge overhaul to change the relationship. I would not be able to add the relevant standard ID to the Pack table - which would be required to bind the relationship. Additionally, the Wine/Pack scenario is fictional, created for simplicity. My real life scenario is much more complicated than that.Thanks, but I am not after an alternative solution. I just need to know how to bind a relationship by one field and then another if no relationships exist – Typhoon101 Jul 02 '19 at 19:33
  • I see. I'm not much use then anymore without further research. Any solution I know involves modifying the database. It's difficult to create a solution in Eloquent without having some sort of delimiter that tells you which type of resource you're connecting to, and using that to determine which foreign key column to use. Basically polymorphism. [More info here.](https://stackoverflow.com/a/441111/580587) – Aken Roberts Jul 02 '19 at 19:49
  • No problem. thanks anyway. – Typhoon101 Jul 03 '19 at 11:49

0 Answers0