0

当我尝试使用该AJAX选项获取数据时,我收到一条错误消息,指出列不存在。

我使用的主表是users,另一个是orders. 在User模型中,我在两个表之间设置了一个关系。在这种情况下,该orders表具有一个user_idas FK。

public function orders()
{
    return $this->hasMany(Order::class);
}

UserCrudController我设置order列以使用访问器获取数据:

[
    'label' =>  'Orders',
    'name'  =>  'order_count',
],

// This method is in the `User` model
public function getOrderCountAttribute() {
    return count($this->orders);
}

因此,当我启用时,AJAX我得到的错误是该列在表order_count中不存在。User未达到关系和访问器,因为我尝试使用die().

有没有办法可以使用运行查询AJAX?我正在使用背包 3 和 laravel 5.4

4

1 回答 1

1

您应该能够使用model_function列类型。据我所知,对于您的情况,它将是:

[
   // run a function on the CRUD model and show its return value
   'name' => "order_count",
   'label' => "Orders", // Table column heading
   'type' => "model_function",
   'function_name' => 'getOrderCountAttribute', // the method in your Model
   // 'limit' => 100, // Limit the number of characters shown
]

希望能帮助到你!

于 2018-07-11T15:09:06.990 回答