我正在尝试使用Eloquent ORM在Laravel 5.6应用程序中实现多对多关系。我有Customer
和CustomerGroup
模型,分别与同名的数据库表和数据透视表customers_customergroups
相对应。 Laravel的文档中提供了与用户和角色相对相同的案例。 customers_customergroups
表如下所示:
+-------------+------------------+------------+------------+
| customer_id | customergroup_id | created_at | updated_at |
+-------------+------------------+------------+------------+
| 53210 | 2 | -- | -- |
| 53210 | 4 | -- | -- |
| 53211 | 3 | -- | -- |
| 53211 | 4 | -- | -- |
+-------------+------------------+------------+------------+
我的Customer
模型有一个称为customerGroups()
的方法,该方法检索客户所属的所有组。
public function customerGroups()
{
return $this->belongsToMany('App\CustomerGroup', 'customers_customergroups', 'customer_id', 'customergroup_id');
}
检索工作正常,但是当我尝试通过将Customer
对象作为数组传递给Request
Eloquent方法来更新update()
时,必须在同一存储库方法中更新customers_customergroups
表,后者正常更新,但是Customer::find($id)->update($request->toArray())
引发异常:
SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'groups'。...
在传递给groups
的Request对象中没有名为Customer::find($id)->update();
的参数
这里会发生什么,为什么会引发此异常,我在做什么错,以及如何正确处理呢?
提前谢谢!
编辑(在以下评论中发布所需的日志):
[2019-05-14 13:42:11] local.INFO: array (
'id' => 53211,
'name' => 'CUSTOMER ONE',
'surname' => NULL,
'Telephone' => '0123-4567890',
'email' => 'email@example.com',
'Street' => '22 BAKER STR.',
'City' => 'LONDON',
'Postal_code' => '1234',
'is_active' => '1',
'website' => '',
)
编辑(添加HCK请求的代码):
Customer.js
$scope.CreateCustomer = function(isValid) {
if (isValid)
{
PostData = {
'id': $scope.customer_id,
... all other parameters from the previous edit ...
}
if(($scope.izbraniGrupi).length > 0)
{
PostData.izbraniGrupi = $scope.groupsSelected;
}
$http({
method : 'PUT', // Laravel controller is a resource
url : 'customers',
data : PostData
}).then(function mySuccess(response) {
// Other stuff that happens on responseCode == 200.
});
};
CustomerController.php
// $this->repository is defined normally in __construct() and works as supposed to.
public function update(Request $request, $id)
{
try {
$this->repository->update($id, $request);
} catch(\Exception $e) {
return response()->json([
'message' => $e->getMessage()
]);
}
return response()->json([
'message' => 'Customer updated'
]);
}
CustomerRepository.php
public function update($id, $data)
{
if(isset($data['izbraniGrupi'])) {
DB::table('customers_customergroups')->where(['customer_id' => $id])->delete();
foreach ($data['izbraniGrupi'] as $group) {
DB::table('customers_customergroups')->insert([
'customer_id' => $id,
'customergroup_id' => $group['id']
]);
}
unset($data['izbraniGrupi']);
}
Customer::find($id)->update($data->toArray());
}
答案 0 :(得分:1)
我认为问题与完整的Request类的使用有关,而不仅仅是获取有效负载。试试这个:
CustomerController.php
...
$this->repository->update($id, $request->all());
// or even better: ^^^^^^^^
$this->repository->update($id, $request->only('the', 'fields', 'that', 'you', 'expect'));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
不相关,但我注意到您正在存储库中执行此操作:
...
DB::table('customers_customergroups')->where(['customer_id' => $id])->delete();
foreach ($data['izbraniGrupi'] as $group) {
DB::table('customers_customergroups')->insert([
'customer_id' => $id,
'customergroup_id' => $group['id']
]);
}
...
在我看来,您正在分离所有相关记录,以便将客户组重新附加到 customers 。对于Syncing Associations,这似乎是一个很好的例子:
Customer::find($id)->customerGroups()->sync(collect($data['izbraniGrupi'])->pluck('id'));