【发布时间】:2019-02-16 02:07:41
【问题描述】:
我通过以下方式获取 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);
任何帮助将不胜感激。
提前致谢
【问题讨论】:
-
你解除这些关系的目的是什么?
-
我必须拥有相同字段的表。所以我需要根据值检查不同的列。因此,如果外键的值相同,则需要取消设置该关系
-
您有哪些具有相同字段的表?请举个例子好吗?
-
$product->merchant = null怎么样? -
如果我不想要价值,那应该可以。但我需要取消设置密钥。