eloquent-json-relations: eager load use with method,show error “array_key_exists() expects parameter 2 to be array, null given”
My Code as follows:
class CourseCategory extends Model
{
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
public function teachers()
{
return $this->hasManyJson(Teacher::class, 'courses[]->category');
}
}
class Teacher extends Model
{
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
protected $casts = [
'courses' => 'json',
];
public function courseCategories()
{
return $this->belongsToJson(CourseCategory::class, 'courses[]->category');
}
}
the json column data sample:
[{"main_category":21,"category":33,"course":[85]},{"main_category":20,"category":25,"course":[88]},{"main_category":37,"category":40}]
When I use CourseCategory::find(33)->teachers , is ok,
but I use CourseCategory::with('teachers')->find(33) , show error array_key_exists() expects parameter 2 to be array, null given
I check vendor/staudenmeir/eloquent-json-relations/src/Relations/HasManyJson.php:221
protected function pivotAttributes(Model $model, Model $parent)
{
$key = str_replace('->', '.', $this->key);
$record = collect($model->{$this->getPathName()})
->filter(function ($value) use ($key, $parent) {
return Arr::get($value, $key) == $parent->{$this->localKey};
})->first();
return Arr::except($record, $key);
}
I found $parent->{$this->localKey} is null
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 28 (11 by maintainers)
yew
There’s still a mistake