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;