i have two model
this is my material model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class material extends Model
{
protected $fillable = ['proposal_id','thickness','width','length','qty','description'];
public $timestamps = false;
protected $appends = ['total'];
public function proposal()
{
return $this->belongsTo(proposal::class);
}
public function getTotalAttribute()
{
return $this->qty * $this->price;
}
}
this is my proposal model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class proposal extends Model
{
public function materials()
{
return $this->hasMany(material::class);
}
}
i'm trying to get all proposal with summary from total attribute which is an appended attribute.
This is not working:
\App\proposal::with(['materials'=>function($q){
$q->sum('total');
}])
->get();
how to do it properly? thank you