vendredi 8 novembre 2019

How to get foreign key's method used in Laravel Eloquent Relationships?

I have a problem with how to get the method name that was used in other models.

There is a list of models and some of them have used different method name on its relationship to other models.

For example, I have a model name of Member Rate Detail wherein it belongs to Member Rate model. The method that connects from Member Rate Detail to Member Rate is head() method.

Here is the sample code for head() method:

public function head()
{
    return $this->belongsTo(MemberRate::class, 'member_rate_head_id')->withTrashed();
}

And for Customer Detail model it belongs to Customer model. And the connector method name that was used is group()

Here is the sample code for group():

public function group()
{
    return $this->belongsTo(Customer::class, 'head_id', 'id')->withTrashed();
}

So the problem is I don't know if this model is using head() or group() or another method name.

Is there a Laravel Relationship concept way which can get a list or an array type of its foreign key's method used?

I'm expected to get the method name so that I can direct it to its instance class.

For example:

$memberRateDetail->getForeignMethod()->created_by;

**OR**

$customerDetail->getForeignMethod()->created_by;

Thank you so much!!!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire