Correct way to drop an assignment
Created 6 years ago by drmonkeyninjaI'm needing to drop some assignments from a stream. What is the correct way of doing this?
Using a regular migration (assuming this is what you're after - using migrations) you can do something like this:
$field = $this->fields()->findBySlugAndNamespace('foo', 'bar');
$stream = $this->streams()->findBySlugAndNamespace('foo', 'bar');
$this->assignments()->delete($this->assignments()->findByFieldAndStream($field, $stream));
Of course this is "to the T".. You can just do $this->assignments()->findByFieldAndStream($field, $stream)->delete();
since it's Eloquent.
That seems to fail for me Ryan. I get the following error when I try and run the migration:-
Type error: Argument 1 passed to Anomaly\Streams\Platform\Model\EloquentRepository::delete() must be an instance of Anomaly\Streams\Platform\Model\EloquentModel, null given
Sorry Ryan, re-reading your response I'm a little confused as you seem to just give the same example twice, or am I missing the point?. The error I'm getting appears to be because there is no findByFieldAndStream() function in my version of Pyro. Has this recently been added?
I had it backwards - if you pop into the API there you can see it's actually:
public function findByStreamAndField(StreamInterface $stream, FieldInterface $field);
Yes the same example twice to demonstrate a point that it's not something special I guess. You can delete Eloquent models like $model->delete()
or you can use our repository system. I like to mention to encourage people getting out of the habit of "the Pyro way" when "the Laravel" way is the same or similar and works OK too.
Take a peak at the \Anomaly\Streams\Platform\Assignment\AssignmentRepository
as well as the one for streams and fields to get an idea of what I am talking about. Vs for example the AssignmentModel
which is just an Eloquent model.
Hope that makes sense - I could be rambling...
Thanks Ryan, that's done the trick.
I had it backwards - if you pop into the API there you can see it's actually:
Yes the same example twice to demonstrate a point that it's not something special I guess. You can delete Eloquent models like
$model->delete()
or you can use our repository system. I like to mention to encourage people getting out of the habit of "the Pyro way" when "the Laravel" way is the same or similar and works OK too.Take a peak at the
\Anomaly\Streams\Platform\Assignment\AssignmentRepository
as well as the one for streams and fields to get an idea of what I am talking about. Vs for example theAssignmentModel
which is just an Eloquent model.Hope that makes sense - I could be rambling...