雄辩:关系

时间:2017-08-28 16:52:06

标签: php sql laravel eloquent

我对两张桌子之间的关系有很大的疑问 我想将订阅链接到这样的订单:

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

但没有任何工作,我尝试了一切。

订阅表enter image description here

订购表enter image description here

我试过这个模型,但它不起作用:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Subscription extends Model
{
    public $timestamps = false;

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

订单型号:

<?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');
    }
}

错误:

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;

感谢您的

1 个答案:

答案 0 :(得分:1)

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

Public Sub g_MailMerge(strTemplate As String)
    Dim strPath As String
    Dim strQuery As String

    Dim doc As Word.Document
    Dim wrdApp As Word.Application

    On Error GoTo g_MailMergeError

    DoCmd.Hourglass True
   'Delete the rtf file