hasMany relation ship, to get data from one to one relation model

Posted 11 months ago by Stank0V01

Hello i have 3 tables, so here it

sms\
:id
:price
:servID

Product\
:id
:name
:sms_id
:description

Orders\
:id
:product_id
:user_id

What i want? I want to get all products from orders and sum price of products (it depend ot sms price so i use sms_id in product, i call it like this $product->sms->price)

How this will be done?

Here is my models:

class Product extends Model
{
    protected $fillable = [];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function orders(){
        return $this->hasMany(Order::class);
    }
    public function sms() {
        return $this->hasOne(Sms::class,'id','sms_id');
    }

}

class Orders extends Model 
{
    public function product() {
        return $this->belongsTo(Product::class);
    }
}

class Sms extends Model
{
    public function product() {
        return $this->belongsTo(Product::class);
    }
}


Please sign in or create an account to participate in this conversation.