如何在laravel的控制器中使用附加属性?

如何在laravel的控制器中使用附加属性?,laravel,Laravel,这是我添加了order属性的模型 protected $appends = [ 'order' ]; public function getOrderAttribute(){ return $this->getOrder()-this->cancelOrder(); } 这是我的控制器,它将在其中返回产品数组。但我想选择产品名称和订单,因为在数据库中找不到订单列,它会抛出错误 $products=Product::select('produ

这是我添加了order属性的模型

protected $appends = [
  'order'
];

    public function getOrderAttribute(){
       return $this->getOrder()-this->cancelOrder();
      }
这是我的控制器,它将在其中返回产品数组。但我想选择产品名称和订单,因为在数据库中找不到订单列,它会抛出错误

 $products=Product::select('product_name','order')->get();
   return response()->json([
    'data'=>$products
   ])

是,该列在表中不存在,因此您无法选择它。。。这就是sql的工作原理。。。您在模型的
$appends
中添加了
订单的“访问器”,因此它将包含在模型的序列化版本(JSON)中