dshdb64088 2014-04-08 17:37
浏览 56
已采纳

Laravel ORM调用范围函数关系

I am trying to load a relationship from my customer table into my orders table but every time I try I keep getting this error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException Call to a member function where() on a non-object

Here is my model for the orders

class Orders extends Eloquent
{
    protected $softDelete = true;

    public function order_items()
    {
        return $this->hasMany('Order_item');
    }

    public function customer()
    {
        return $this->hasOne('Customer', 'id', 'customer_id');
    }

    public function client()
    {
        return $this->hasOne('Client', 'id', 'client_id');
    }

    public function billingAddress()
    {
        if($this->client()->first() != null)
        {
            return $this->client()->getBillingAddress($this->billing_address_id);
        }
        else
        {
            return $this->customer()->getBillingAddress($this->billing_address_id);
        }
    }
}

Here is my customer model

class Customer extends Eloquent
{
    protected $softDelete = true;

    public function order()
    {
        $this->belongsTo('Orders');
    }

    public function addresses()
    {
        $this->hasMany('Customeraddress', 'id', 'customer_id');
    }

    public function scopeGetBillingAddress($query, $addressId)
    {
        return $this->addresses()
                    ->where('id', '=', $addressId)
                    ->where('type', '=', 'billing');
    }

    public function scopeGetShippingAddress($query, $addressId)
    {
        return $this->addresses()
                    ->where('id', '=', $addressId)
                    ->where('type', '=', 'shipping');
    }
}

And finally here is my customeraddress model:

class Customeraddress extends Eloquent
{
    protected $table = "customer_addresses";
    protected $softDelete = true;

    public function customer()
    {
        $this->belongsTo('Customer');
    }
}

Now I am trying to run this on my controller to get the order address but I keep getting errors. How would I be able to do that via Eloquent relationship and scope function?

$address = Orders::find(21)->billingAddress()->first();
echo $address->street;
  • 写回答

1条回答 默认 最新

  • dongtun1209 2014-04-08 17:42
    关注

    Change this one:

    public function addresses()
    {
        // $this->hasMany('Customeraddress', 'id', 'customer_id'); // wrong order of keys
        return $this->hasMany('Customeraddress', 'customer_id', 'id');
    }
    

    and these two

    // edit: Order is a child of customer and client so:
    public function customer()
    {
        //return $this->hasOne('Customer', 'customer_id', 'id');
        return $this->belongsTo('Customer', 'customer_id', 'id'); // customer_id and id are redundant bu I leave it for clarity
    }
    
    public function client()
    {
        //return $this->hasOne('Client', 'client_id', 'id');
        return $this->belongsTo('Client', 'client_id', 'id'); // same here
    }
    

    And by the way, this one is dangerous :)

    public function billingAddress()
    {
        // This line results in db query everytime you call billingAddress 
        // Unless you cache the query, it's overkill
        if($this->client()->first() != null)
    
        // instead use:
        if($this->client != null)
    
        {
            return $this->client()->getBillingAddress($this->billing_address_id);
        }
        else
        {
            return $this->customer()->getBillingAddress($this->billing_address_id);
        }
    }
    

    change scopes to accessors:

    public function getBillingAddressAttribute($addressId)
    {
        return $this->addresses()
                    ->where('id', '=', $addressId)
                    ->where('type', '=', 'billing')->first();
    }
    
    public function getShippingAddressAttribute($addressId)
    {
        return $this->addresses()
                    ->where('id', '=', $addressId)
                    ->where('type', '=', 'shipping')->first();
    }
    
    // then you can call it like $customer->shippingAddress etc
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)