首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 5.6.5关系执行错误查询

Laravel 5.6.5关系执行错误查询
EN

Stack Overflow用户
提问于 2018-03-07 16:26:09
回答 1查看 289关注 0票数 0

正如你在下图中所看到的,我的购物单和购物单工艺路线步骤计划之间的早期关系并不是必须的。

我不知道我到底做错了什么,所以我希望有人能帮助我。在下面的代码中,为了使代码更易读,我在代码中去掉了一些字段。

代码语言:javascript
复制
class shoporder extends Model
{
    protected $primaryKey = 'ID';

    protected $fillable = [
        'CADDrawingURL',
        'ID',
        'Costcenter',
        'CostcenterDescription',
        'Costunit',
        'CostunitDescription',
        'Created',
        'Creator',
        'CreatorFullName',
        'Description',
        'ShopOrderParent',
        'ShopOrderParentNumber',
        'ShopOrderRoutingStepPlanCount',
        'Status',
        'SubShopOrderCount',
    ];

    public function shopOrderRoutingStepPlans() {
        return $this->hasMany('App\shopOrderRoutingStepPlan', 'ShopOrder', 'ID');
    }
}

class ShopOrderRoutingStepPlan extends Model
{
    protected $primaryKey = 'ID';
    public $table = "shoporderroutingstepplans";
    protected $fillable = [
        'Account',
        'ID',
        'AccountName',
        'AccountNumber',
        'AttendedPercentage',
        'Backflush',
        'Created',
        'Creator',
        'CreatorFullName',
        'Description',
        'ShopOrder',
    ];

    public function shopOrder() {
        return $this->belongsTo('App\shopOrder', 'ShopOrder', 'ID');
    }
}

这是我正在执行的代码,用于在控制器中获取1个商店订单之间的关系。

代码语言:javascript
复制
$orders = shopOrder::find('0600959e-6b92-4135-8ea8-1fa2fd92a916')->shopOrderRoutingStepPlans()->get();

在商店订单迁移中,我定义了主键:

代码语言:javascript
复制
$table->string('ID')->unique();
$table->primary('ID');

在shoporderroutingstepplans迁移中,我按如下方式定义了外键。

代码语言:javascript
复制
$table->string('ID')->unique();
$table->primary('ID');

$table->foreign('ShopOrder')
      ->references('ID')
      ->on('shoporders');
EN

回答 1

Stack Overflow用户

发布于 2018-03-07 16:32:55

您必须切换最后两个参数的顺序:

从…

代码语言:javascript
复制
 return $this->hasMany('App\shopOrderRoutingStepPlan', 'ShopOrder', 'ID');

代码语言:javascript
复制
 return $this->hasMany('App\shopOrderRoutingStepPlan', 'ID', 'ShopOrder');

这些参数包括

  1. 模型,链接模型中列的
  2. 名称,此模型中列的
  3. 名称。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49147206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档