Support the ongoing development of Laravel.io →
posted 11 years ago
Eloquent

I have a model where current_order_workflow_step is optional order_workflow_step is basially a join table to workflow_step, but has a few extra columns I want to define a relationship to get the current_workflow_step, but the trick is that the current_order_workflow_step isn't always set, so I'm not sure how is the best way to write it to allow for that. This is my attempt:

public function current_order_workflow_step()
{
	return $this->belongsTo('OrderWorkflowStep','current_order_workflow_step_id');
} 


public function current_workflow_step() 
{
        if($this->current_workflow_step_id)
        {
            return $this->current_order_workflow_step()->with('workflow_step')->first()->workflow_step;
        } 
        else 
        {
            return $this->current_order_workflow_step();
        }
}

That seems to be working, but I'm not sure if there is a way to do this more properly?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

thedamon thedamon Joined 2 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.