I have a model Publisher
that has a many to many relationship to Collection
Both have a property called approved
that is set to false when initially created
I have a global scope that hides any Publisher
or Collection
where approved === false
public function apply(Builder $builder, Model $model)
{
$columnName = $this->tableName ? $this->tableName . '.approved' : 'approved';
$builder->where($columnName, '=', true);
}
Publisher
is set up as follows:
public static function boot()
{
parent::boot();
self::creating(function (Publisher $publisher) {
$publisher->creator_user_id = Auth::user()->id;
});
self::created(function (Publisher $publisher) {
$collection = Collection::create([
'approved' => (bool)$publisher->approved,
'title' => '',
]);
$collection->publishers()->sync($publisher->id);
});
The problem is that the Collection::create
works, but does not return the created Collection
when the global scope is applied. It throws the following error:
No query results for model [App\Collection] 12
The 12 is the ID of the Collection
created. It is in the DB as expected, so that part works.
I have tried applying withoutGlobalScope(ApprovedScope::class)
on the create statement both before and after the create()
but that doesn't seem to help.
The weird thing is that $publisher = Publisher::create($request->all());
in my controller does return the created Publisher
even though it has the same global scope applied to it
Any ideas?
Update: I have narrowed down the issue and it is caused by Collection
using the koenhoeijmakers/laravel-translatable package. So I am still looking for a way to ignore the scope on this create statement.
After careful investigation and some help, it came to light this was the result of the koenhoeijmakers/laravel-translatable
package on the Collection
I have solved the issue by not applying the middleware that applies the ApprovedScope
on the store
routes where this was causing issues.