Closed
Description
My project laravel version is v5.1.19.
I think this issue is related to #8972.
class Custom extends Pivot
{
protected $casts = [
'json' => 'array',
];
}
class User extends Model
{
public function products()
{
return $this->belongsToMany(Product::class)->withPivot('json');
}
public function newPivot(Model $parent, array $attributes, $table, $exists)
{
if ($parent instanceof Product) {
return new Custom($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
}
class Product extends Model
{
public function users()
{
return $this->belongsToMany(User::class)->withPivot('json');
}
public function newPivot(Model $parent, array $attributes, $table, $exists)
{
if ($parent instanceof User) {
return new Custom($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
}
$user = User::find(1);
$product = $user->products->first();
$json = $product->pivot->json;
# Expect array ['category' => 'sample']
# Result string {"category":"sample"}
Thanks.
Metadata
Metadata
Assignees
Labels
No labels