我正在尝试实现belongsToMany关系,我需要在其中列出产品,并在其名称之后,在3个表,订单,描述和order_product之间 Build 关系 .
你可以意识到这个领域似乎是空的 .
这是我的belongsToMany关系:
Description Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Order;
class Description extends Model
{
protected $table = 'erp_product_description';
protected $primaryKey = 'erp_productid';
protected $fillable = ['erp_name'];
public $timestamps = false;
public function product()
{
return $this->belongsTo('App\Product', 'erp_productid','erp_productid');
}
public function order()
{
return $this->belongsToMany('App\Order', 'erp_order_product', 'erp_productid', 'erp_createdid');
}
}
Order Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
protected $table = 'erp_order';
protected $fillable = ['erp_createdid'];
public function description()
{
return $this->belongsToMany('App\Description', 'erp_order_product', 'erp_createdid', 'erp_productid');
}
}
我使用Laravel'DebugBar'插件来查看正在运行的查询并返回此信息:
select `erp_product_description`.*, `erp_order_product`.`erp_createdid` as `pivot_erp_createdid`, `erp_order_product`.`erp_productid` as `pivot_erp_productid` from `erp_product_description` inner join `erp_order_product` on `erp_product_description`.`erp_productid` = `erp_order_product`.`erp_productid` where `erp_order_product`.`erp_createdid` is null
为什么我的查询的值为null?有什么建议吗?