Replies: 1 comment
-
Hey @anthonytp, I just ran into the same problem and debugged it for ages :D Is it possible that you use any of the static methods (like Model::getStatesFor()) before actually instantiating the model itself (e.g. in your FormRequest)? While debugging I've seen that in my case, the static $stateFields property of the HasStates trait got initialized before booting the model, which caused it to be an empty array. And since it was empty the listeners weren't registered to unserialize the state. I used the following code in my FormRequest class to validate incoming status changes: Rule::in(Preorder::getStatesFor('status')->map(fn ($state) => StatusState::resolveStateName($state))) Added this so that the docs generator I use automatically adds the available states to the documentation. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I'm having an issue when using the model states. I don't think it's a bug but it's not behaving as expected, although only in one of my controllers. We are using the older version of the code and, for now, cannot update to the latest version.
The problem is the "state" field is not being cast to a Spatie\ModelStates\State derived object and is returned as a string. So when I try to transition to a new state I get the exception: "Call to a member function transitionTo() on string".
However, there are other parts of the code where the same model is used and transitions work correctly, with the state being converted to the correct class. I just can't work out why this one controller in the code is causing problems.
States derived from my own abstract class
Even though I register the states in the base class they are also in the same folder. A migration added the
status
field to the database tableand my model implements HasStates
The problem is occurring in a controller which I am updating. It currently handles an API call to create a new
ShiftPattern
and attach it to aBooking
model, which works. There is a one-to-many relationship defined betweenBooking
andShiftPattern
.The new ShiftPattern is being created in the "Pending Approvel" state, but some bookings do not require them to be "approved", so I want to move them straight to the "Approved" state.
But I keep getting the error "Call to a member function transitionTo() on string" which happens inside the
transitionTo
call once the state field has been resolved by the library. As I said, in other cases this works fine, but in this one controller the state field is not automatically cast to an object.I thought the model might not be "booted" correctly to set up the class casts, so I exposed a function to allow the controller to call
bootIfNotBooted
but inside that it skips the initialisation as it has been booted.Then I tried refreshing and reloading the ShiftPattern from the database, to see if that would solve it:
neither worked, the state field was still being returned as a string.
I also tried creating the model separately from associating it with the Booking, but that did not fix the issue either
Does anyone have any idea why this could be happening? I really don't think it's a bug in the ModelStates library since it works in tinker and other sections of my code, it seems to be a Laravel attribute cast issue.
Beta Was this translation helpful? Give feedback.
All reactions