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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?