亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

3 個表 Laravel 之間的關系

3 個表 Laravel 之間的關系

PHP
慕工程0101907 2021-07-13 15:02:16
我正在嘗試創建一個 API 資源,我可以在其中查看來自三個表的響應構建。店鋪命令購買目前,我可以使用路由 (api/store/orders) 去商店(有多個商店)并查看 JSON 響應中的訂單?;貜蛖 data: [  {   id: 1,   code: "1f",   status: "waiting",   user_name: "Salman",   created_at: "",   updated_at: ""  },  {   id: 2,   code: "2f",   status: "waiting",   user_name: "Jerome",   created_at: "",   updated_at: ""  } ]}但是,當我嘗試將我的購買添加到響應中時:namespace App\Http\Resources;use Illuminate\Http\Resources\Json\JsonResource;class Orderresource extends JsonResource{    public function toArray($request)    {        return [            'id' => $this->id,            'code' => $this->code,            'status' => $this->status,            'user_name' => $this->user_name,            // Added purchases            'order' => $this->purchases,            'created_at' => (string) $this->created_at,            'updated_at' => (string) $this->updated_at,        ];    }}我收到錯誤響應 Undefined property: stdClass::$purchases現在我已經將購買添加到我的訂單模型中:  public function purchases()  {    return $this->hasMany(Purchase::class);  }但是,我知道訂單可能取決于我的 show_order_per_store.在我的功能 OrderController    public function show_order_per_club($id)    {      $order = DB::table('orders')->where('store_id', '=', $id)->get();      return  Orderresource::collection($order);    }現在,此函數獲取所有類似商店的訂單,但如何將購買添加到商店 API 響應中?簡而言之,我試圖獲得每個商店的 API 響應,其中包含它所擁有的訂單,并且購買屬于該訂單。
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

DB:table嘗試直接使用模型,而不是用于查詢:

Order::where('store_id', '=', $id)->get();

似乎DB:table返回stdClass對象,因此它缺少模型可能提供的所有虛擬屬性。


查看完整回答
反對 回復 2021-07-16
  • 1 回答
  • 0 關注
  • 186 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號