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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程