Php Laravel Eloquent返回一个关于归属关系的空集合
更新:这里提到的问题是由XAMPP使用MariaDB而不是MySQL引起的。我已经按照答案将它切换到MySQL,它就像一个魅力Php Laravel Eloquent返回一个关于归属关系的空集合,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,更新:这里提到的问题是由XAMPP使用MariaDB而不是MySQL引起的。我已经按照答案将它切换到MySQL,它就像一个魅力 这是关于一个电子商务平台 我有两个数据表,其中一个连接表用于多对多连接。这个想法是让产品在任何给定的时间都有许多特别优惠 桌子 产品 +-------+-------------------------------+ | id | name | +-------+------------------------
这是关于一个电子商务平台 我有两个数据表,其中一个连接表用于多对多连接。这个想法是让产品在任何给定的时间都有许多特别优惠
桌子 产品
+-------+-------------------------------+
| id | name |
+-------+-------------------------------+
| 10001 | Apple iPhone 11 |
| 10002 | Samsung Galaxy S11 |
+-------+-------------------------------+
+----+-------------------------------+
| id | name |
+----+-------------------------------+
| 1 | Awesome Offer |
| 2 | Year End Offer |
+----+-------------------------------+
特别优惠
+-------+-------------------------------+
| id | name |
+-------+-------------------------------+
| 10001 | Apple iPhone 11 |
| 10002 | Samsung Galaxy S11 |
+-------+-------------------------------+
+----+-------------------------------+
| id | name |
+----+-------------------------------+
| 1 | Awesome Offer |
| 2 | Year End Offer |
+----+-------------------------------+
产品特别优惠
+------------+------------------+----------+
| product_id | special_offer_id | discount |
+------------+------------------+----------+
| 10001 | 1 | 10.0 |
| 10002 | 2 | 12.5 |
+------------+------------------+----------+
class ProductController extends Controller
{
public function index()
{
$product = Product::find(10001);
dd($product->specialOffers);
}
}
模型 由于需求是针对
many-to-many
关系,因此我在模型中使用belongtomy
方法
产品
类产品扩展模型
{
公共职能专员()
{
返回$this->belongtomany(SpecialOffer::class)->withPivot(‘折扣’);
}
}
SpecialOffer
+------------+------------------+----------+
| product_id | special_offer_id | discount |
+------------+------------------+----------+
| 10001 | 1 | 10.0 |
| 10002 | 2 | 12.5 |
+------------+------------------+----------+
class ProductController extends Controller
{
public function index()
{
$product = Product::find(10001);
dd($product->specialOffers);
}
}
class SpecialLoffer扩展了模型
{
公共功能产品()
{
返回$this->belongtomany(Product::class)->withPivot('discount');
}
}
控制器 以下是控制器代码段 ProductController
+------------+------------------+----------+
| product_id | special_offer_id | discount |
+------------+------------------+----------+
| 10001 | 1 | 10.0 |
| 10002 | 2 | 12.5 |
+------------+------------------+----------+
class ProductController extends Controller
{
public function index()
{
$product = Product::find(10001);
dd($product->specialOffers);
}
}
结果 以下是Laravel返回的内容
Collection {#610 ▼
#items: []
}
它运行的查询如下所述
选择“特价”.*、“产品特价”.“产品id”作为“枢轴产品id”.“产品特价”.“特价”作为“枢轴特价”,“产品特价”。“特价”中的“折扣”作为“核心折扣”,来自“特价”中的“内部连接”“产品特价”。“id”=“产品特价”。“特价”id`其中的“产品特价”。“产品id”=10001
这可能有效
class SpecialOffer extends Model
{
public function products()
{
return $this->belongsToMany(Product::class, 'product_special_offer','special_offer_id','product_id');
}
}
这可能行得通
class SpecialOffer extends Model
{
public function products()
{
return $this->belongsToMany(Product::class, 'product_special_offer','special_offer_id','product_id');
}
}
在连接表中创建第三个模型,并添加这两个关系。而且它会起作用
class ProductSpecialOffer extends Model
{
public function products() {
return $this->belongsTo(Product::class);
}
public function specialOffers() {
return $this->belongsTo(SpecialOffer::class);
}
}
在连接表中创建第三个模型,并添加这两个关系。而且它会起作用
class ProductSpecialOffer extends Model
{
public function products() {
return $this->belongsTo(Product::class);
}
public function specialOffers() {
return $this->belongsTo(SpecialOffer::class);
}
}
用pivot('product_id','special_offer_id')更改此
@ZainFarooq仍然是一样的:-(试试这个$this->belongomany(SpecialOffer::class,'product\u special\u offer');
和$this->belongomany(product::class,'product\u special\u offer'))
@ZainFarooq我以前尝试过指定表名,但在您提到后现在也尝试过。仍然是一样的。MySQL和MariaDB是可以互换的,用一个替换另一个不会改变任何东西,特别是对于一个非常基本的查询,就像您正在运行的查询一样。使用pivot('product\u id','special\u offer\u id')更改此
@ZainFarooq仍然是一样的:-(试试这个$this->belongomany(SpecialOffer::class,'product\u special\u offer');
和$this->belongomany(product::class,'product\u special\u offer'))
@ZainFarooq我以前尝试过指定表名,但在您提到之后,我现在也尝试过指定表名。仍然是一样的。MySQL和MariaDB是可互换的,用一个替换另一个不会改变任何事情,特别是对于像您正在运行的一个非常基本的查询。