我通过以下方式获取Laravel Eloquent系列:
$product = Product::query()->with(['merchant', 'picture'])->where('id', $id)->first();
并得到 $product
的转储
Product {
#casts: ...
#dates: ...
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:1 [
"id" => 27
]
#original: ...
#changes: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [
"merchant" => Merchant {...}
"picture" => Picture {...}
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: ...
}
我需要从这个集合中取消设置关系对象 merchant
和 picture
.
我尝试过以下选项但失败了:
unset($product['merchant']);
unset($product->merchant);
任何帮助将不胜感激 .
提前致谢
2 回答
在Laravel 5.6.25中,您可以使用
unsetRelation()
:在那之前:
如果模型中有
merchant
属性(表中的merchant
列),则可以使用$product->getOriginal('merchant')
或$product->getAttribute('merchant')
获取值