0

Class ModelA has relationship belongsTo to ModelB. Is there any way to access that attribute from ModelA? Something like:

$this->model_b->model_b_attribute;

Also, is there a way to chain model to attribute? If I have belongsTo relationship from ModelB to ModelC could I do this:

$this->model_b->model_b_attribute->model_c;

Edit:

My code:

ModelA would be:

class LeaseTenant extends Model {

    protected $appends = ['is_deposit_paid'];

    public function lease_request()
    {
        return $this->belongsTo('App\Models\LeaseRequest');
    }

    public function getIsDepositPaidAttribute()
    {
        return $this->email == $this->lease_request->security_deposit_entry->bank_account->user->email;    
    }
}

And ModelB:

class LeaseRequest extends Model {

    protected $appends = ['security_deposit_entry'];

    public function getSecurityDepositEntryAttribute()
    {
        return Rent
                 ::where('property_id', $this->property_id)
                 ->where('lease_request_id', $this->id)
                 ->where('type', 'security_deposit')
                 ->orderBy('created_at', 'asc')->first();
    }
}

I want to access Rent table from LeaseTenant.

6
  • post your code please Commented May 20, 2019 at 16:40
  • @DiegoCespedes I am not sure how this can help, but I have posted my code Commented May 20, 2019 at 16:46
  • @niksrb it is important because it helps to understand your actual problem. Commented May 20, 2019 at 16:50
  • Thanks, I wanted to create attribute in ModelA to access column from ModelC over attribute from ModelB. And it is possible! Thanks agaub Commented May 20, 2019 at 16:57
  • In your LeaseRequest model, on the query inside your getSecurityDepositEntryAttribute() method I think that search by property_id is redundant given the fact that the LeaseRequest seems to belong to a unique Property. Now, the type used in the Rent is a static one, does this will apply for every LeaseRequest? If so, you could create a relationship directly between LeaseRequest and Rent instead of doing the query manually. But anyway, I'm glad I could help. Commented May 20, 2019 at 16:59

1 Answer 1

1

If you have a relationship belongsTo between ModelA and ModelB:

# ModelA.php

public function modelB()
{
    return $this->belongsTo(ModelA::class);
}

Then you could also access the relationship to get the ModelA instance, from which you can access the ModelA attributes.

$modelA = ModelA::find(1);
$name = $modelA->modelB->name;
//                      ^^^^^^ modelB attribute

Also, if you have another belongsTo relationship inside ModelB, you can do this:

$name = ModelA::find(1)->modelB->modelC->name;
//                                      ^^^^^^ modelC attribute
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.