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

t0berius's avatar

accessor in route model binding

Since I know it's impossible to use an Accessor inside a route model binding (since model binding works on SQL layer directly) I would like to get some hints / ideas how to solve a problem in an elegant way.

my route model binding:

    Route::bind('disputableOrder', function ($value) {
        return \App\Order::where('seller_id', \Auth::id())->orWhere('buyer_id', \Auth::id())->where('id', $value)->where('can_dispute', true)->with(['xy'])->firstOrFail();
    });

Here's my can_dispute accessor, so you can get a idea of it:

public function getCanDisputeAttribute(){

    //seller "view"
    if(\Auth::id() == $this->seller_id){

        if(in_array($this->status_id, [4, 5, 8]))
            return true;
        else
            return false;
    }
    //buyer "view"
    else{
        if(in_array($this->status_id, [2, 9, 11, 43,  ]))
            return true;
        else
            return false;

    }
}

You may see I work with status_id fields, to store the current status of an order. I would like to keep the id's mostly on the model itself, instead of "hardcoding" them in every controller / inside the route model binding itself.

Any suggestions how to proceed, without generating that much double code?

0 likes
1 reply
devfrey's avatar

I'm not sure I understand your question. If your table doesn't have a can_dispute column, you won't be able to perform a where() on it. Accessors are really meant for mutating data that is already present on the model.

Please or to participate in this conversation.