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

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

當數據庫中有 1 條記錄時,Laravel 顯示重復項

當數據庫中有 1 條記錄時,Laravel 顯示重復項

PHP
犯罪嫌疑人X 2023-07-30 13:54:55
我是 Laravel 的初學者。我在使用 Laravel 時遇到了一些小問題。我有這個代碼:$categories = $categoryRepository->descendantsAndSelf($slug ?? null); 這個返回:Illuminate\Support\Collection {#535 ▼  #items: array:17 [▼    0 => 1    1 => 2    2 => 3    3 => 4    4 => 5    5 => 6    6 => 7    7 => 8    8 => 9    9 => 10    10 => 11    11 => 12    12 => 13    13 => 14    14 => 15    15 => 16    16 => 17  ]}還有我的存儲庫:public function getProductFromIdCategories($categories)    {        return $this->model->select('name', 'slug', 'products.id', 'small_description')->with(['selectedCategory', 'frontImage'])->active()->leftJoin('selected_product_categories', function ($join) {            $join->on('products.id', 'selected_product_categories.product_id');        })->whereIn('selected_product_categories.category_id', $categories)->get();      }當我運行我的代碼時:dd($this->model->getProductFromIdCategories($categories->toArray()));
查看完整描述

1 回答

?
當年話下

TA貢獻1890條經驗 獲得超9個贊

您有關系“selectedCategory”。那么為什么要使用左連接呢?使用此代碼


public function getProductFromIdCategories($categories)

{

    return $this->model->select('name', 'slug', 'products.id', 'small_description')

    ->with(['selectedCategory', 'frontImage', 'selectedCategory' => function($q) use ($categories){

        $q->whereIn('category_id', $categories);

    }])->active()->get();  

}


查看完整回答
反對 回復 2023-07-30
  • 1 回答
  • 0 關注
  • 149 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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