簡體   English   中英

Laravel Eloquent 在 belongsToMany 關系上返回一個空集合

[英]Laravel Eloquent returns an empty collection on belongsToMany relationship

更新:這里提到的問題是由 XAMPP 使用 MariaDB 而不是 MySQL 引起的。 我已按照此處的答案將其切換為 MySQL ,它就像一個魅力。


這是關於電子商務平台的。

我有 2 個數據表和 1 個用於多對多連接的連接表。 這個想法是讓產品在任何給定時間運行許多特別優惠。


產品

+-------+-------------------------------+
| id    | name                          |
+-------+-------------------------------+
| 10001 | Apple iPhone 11               |
| 10002 | Samsung Galaxy S11            |
+-------+-------------------------------+

特別優惠

+----+-------------------------------+
| id | name                          |
+----+-------------------------------+
|  1 | Awesome Offer                 |
|  2 | Year End Offer                |
+----+-------------------------------+

product_special_offer

+------------+------------------+----------+
| product_id | special_offer_id | discount |
+------------+------------------+----------+
| 10001      | 1                | 10.0     |
| 10002      | 2                | 12.5     |
+------------+------------------+----------+

楷模

由於要求是many-to-many關系,因此我在模型中使用belongToMany方法。

產品

class Product extends Model
{
    public function specialOffers()
    {
        return $this->belongsToMany(SpecialOffer::class)->withPivot('discount');
    }
}

特別優惠

class SpecialOffer extends Model
{
    public function products()
    {
        return $this->belongsToMany(Product::class)->withPivot('discount');
    }
}

Controller

以下是 controller 片段。

產品控制器

class ProductController extends Controller
{
    public function index()
    {
        $product = Product::find(10001);

        dd($product->specialOffers);
    }
}

結果

以下是 Laravel 返回的內容。

Collection {#610 ▼
  #items: []
}

它運行的查詢如下所述。

select `special_offers`.*, `product_special_offer`.`product_id` as `pivot_product_id`, `product_special_offer`.`special_offer_id` as `pivot_special_offer_id`, `product_special_offer`.`discount` as `pivot_discount` from `special_offers` inner join `product_special_offer` on `special_offers`.`id` = `product_special_offer`.`special_offer_id` where `product_special_offer`.`product_id` = 10001

請更改您的 model

產品

class Product extends Model
{
    public function specialOffers()
    {
        return $this->belongsToMany('App\SpecialOffer','product_special_offer','special_offer_id','product_id ')
    }
}

特別優惠

class SpecialOffer extends Model
{
    public function products()
    {
        return $this->belongsToMany('App\SpecialOffer','product_special_offer','product_id','special_offer_id');
    }
}
class ProductController extends Controller
{
    public function index()
    {
        $product = Product::find(10001);

        dd($product->name);
    }
}

這可以工作

class SpecialOffer extends Model
{
    public function products()
    {
        return $this->belongsToMany(Product::class, 'product_special_offer','special_offer_id','product_id');
    }
}

在連接表中創建第三個 model,並添加兩個關系。 它會起作用的。

class ProductSpecialOffer extends Model
{
   public function products() {
       return $this->belongsTo(Product::class);
   }

   public function specialOffers() {
       return $this->belongsTo(SpecialOffer::class);
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM