简体   繁体   中英

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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