简体   繁体   English

雄辩的:人际关系

[英]Eloquent: Relationships

I have a big problem with the relation between two tables. 我对两个表之间的关系有很大的疑问。
I want to link Subscription to Orders like this : 我想将订购链接到这样的订单:

$subs = Subscription::first()->order->id

But nothing work i tried everything. 但是没有任何工作,我尝试了一切。

Subscription table : 订阅表 在此处输入图片说明

Order table : 订购表 在此处输入图片说明

I tried this model but it don't work : 我试过这个模型,但是不起作用:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Subscription extends Model
{
    public $timestamps = false;

    public function order()
    {
        return $this->hasOne('App\Order');
    }
}

Order model: 订单型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    public $timestamps = false;

    public function payouts()
    {
        return $this->hasMany('App\Payout');
    }

    public function subscription()
    {
        return $this->belongsTo('App\Subscription');
    }
}

error : 错误:

ErrorException: Trying to get property of non-object in /var/www/vhosts/trafficshield.tools/httpdocs/app/Http/Controllers/CampaignController.php:58

Line 58 : $order_id = Subscription::where('id', '=', 'GjhQpVSdQPirGmEEkJ6pGw')->first()->order->id;

Thank's for your 谢谢你的

因为您使用非自动增量值作为主键,所以必须将其放入模型中:

public $incrementing = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM