title: Laravel - How to sum child data and pass it to parent @foreach
body: <p>i'm new to laravel. So in this case i'm trying to sum child data and pass it to parent nominal column inside parent index @foreach,
  the thing is idk how to declare the parent id inside controller</p>
  <p><a href="https://i.stack.imgur.com/l5aNk.png" rel="nofollow noreferrer">here's my parent index which i want to show the sum child each parent id in @foreach</a></p>
  <p><a href="https://i.stack.imgur.com/z4Ykp.png" rel="nofollow noreferrer">here's my child index that i've already sum it, i want to show it into parent @foreach nominal column as well</a></p>
  <p>here's my <strong>SPJCreateController</strong> which is parent controller</p>
  <pre><code>class SPJCreateController extends Controller
  {
     public function index(Request $request, $id)
     {
        $kegiatan = DPAKegiatan::with('dpatahun')-&gt;findorfail($id);
        $data = SPJCreate::with('spjcreatedetail')
                -&gt;where('id_dpakegiatan', $kegiatan-&gt;id)
                -&gt;orderBy('created_at', 'asc')
                -&gt;paginate(10);
        $total = SPJCreateDetail::where('id_spj', $request-&gt;id)-&gt;sum('nominal');
        return view('spj.kegiatan.create.data', compact('kegiatan','data','total'));
     }
  }
  </code></pre>
  <p>in this controller i declare variable $total which is gonna sum column 'nominal' in SPJCreateDetail Model but idk how to declare <em><strong>where</strong></em> inside of it since $id is the id of grandparent</p>
  <p>It returns 0 inside nominal column if i put <em>$request-&gt;id</em> inside <em>where</em></p>
  <p>however it shown when i call it manually like this</p>
  <pre><code>$total = SPJCreateDetail::where('id_spj', 6)-&gt;sum('nominal');
  </code></pre>
  <p><a href="https://i.stack.imgur.com/ft5Hl.png" rel="nofollow noreferrer">my parent index after i put where id manually</a></p>
  <p>But i don't want that because i want my id to be dynamically called and sum eachrow of it</p>
  <p>My <strong>SPJCreate</strong> Model which is parent model</p>
  <pre><code>class SPJCreate extends Model
  {
      protected $primaryKey = 'id';
      protected $table = 'spj';
      protected $fillable = ['id_dpakegiatan','tipebelanja','tipebelanjadetail'];
  
      public function dpakegiatan()
      {
            return $this-&gt;belongsTo(DPAKegiatan::class, 'id_dpakegiatan');
      }
  
      public function spjcreatedetail()
      {
            return $this-&gt;hasMany(DPAKegiatan::class, 'id');
      }
  }
  </code></pre>
  <p>My <strong>SPJCreateDetail</strong> model which is child model</p>
  <pre><code>class SPJCreateDetail extends Model
  {
       protected $primaryKey = 'id';
       protected $table = 'spj_detail';
       protected $fillable = ['id_spj','id_listbelanja','ket','penerima','nominal','ppn','pph21','pph22','pph23'];
  
       public function spjcreate()
       {
            return $this-&gt;belongsTo(SPJCreate::class, 'id_spj');
       }
  }
  </code></pre>
  <p>Sorry for my bad english i hope u guys can help me because im stuck for a month trying to figure it out :( , Thank you.</p>
  <p>i dd inside my controller</p>
  <pre><code>$total = dd(SPJCreateDetail::where('id_spj', $request-&gt;id)-&gt;sum('nominal'));
  </code></pre>
  <p>it returns like this
  <a href="https://i.stack.imgur.com/omOrg.png" rel="nofollow noreferrer">image</a></p>
  <p>however</p>
  <pre><code>$total = dd(SPJCreateDetail::where('id_spj', 6)-&gt;sum('nominal'));
  </code></pre>
  <p>it return the value correctly
  <a href="https://i.stack.imgur.com/xnSS3.png" rel="nofollow noreferrer">image</a></p>
  <p>here's my web routes</p>
  <pre><code>    // --Here's my grandparent routes-- //
  Route::get('/spj/kegiatan&amp;id={spjkeg}', 'SPJKegController@index')-&gt;name('spjkeg')-&gt;middleware('auth');
      // ------------- //
          
      // --Here's my Parent Route Which is Gonna show my child sum-- //
  Route::get('/spj/kegiatan/create&amp;id={spjcreate}', 'SPJCreateController@index')-&gt;name('spj')-&gt;middleware('auth');
  Route::post('/spj/kegiatan/create/store&amp;id={spjcreate}', 'SPJCreateController@store')-&gt;name('spj.store')-&gt;middleware('auth');
  Route::post('/spj/kegiatan/create/updt&amp;id={spjcreate}', 'SPJCreateController@update')-&gt;name('spj.update')-&gt;middleware('auth');
  Route::delete('/spj/kegiatan/create/del&amp;id={spjcreate}', 'SPJCreateController@destroy')-&gt;name('spj.destroy')-&gt;middleware('auth');
      // ------------ //
                  
      // --Here's the child route-- //
  Route::get('/spj/kegiatan/create/detail&amp;id={spjdet}', 'SPJCreateDetailController@index')-&gt;name('spj.detail')-&gt;middleware('auth');
  Route::post('/spj/kegiatan/create/detail/store&amp;id={spjdet}', 'SPJCreateDetailController@store')-&gt;name('spj.detail.store')-&gt;middleware('auth');
  Route::post('/spj/kegiatan/create/detail/updt&amp;id={spjdet}', 'SPJCreateDetailController@update')-&gt;name('spj.detail.update')-&gt;middleware('auth');
  Route::delete('/spj/kegiatan/create/detail/del&amp;id={spjdet}', 'SPJCreateDetailController@destroy')-&gt;name('spj.detail.destroy')-&gt;middleware('auth');
      // ------------ //
  </code></pre>
  <p>Here's my View.blade.php</p>
  <pre><code>@php $no = 0; @endphp
  @forelse($data as $spj =&gt; $result)
  @php $no++; @endphp
                      &lt;tr&gt;
                        &lt;td class=&quot;text-center&quot;&gt;{{ $spj + $data-&gt;firstitem() }}.&lt;/td&gt;
                        &lt;td class=&quot;text-center&quot;&gt;&lt;b&gt;{{ $result-&gt;tipebelanjadetail }}&lt;/b&gt;&lt;/td&gt;
                        &lt;td class=&quot;text-center&quot;&gt;{{ $total }}&lt;/td&gt;
                        &lt;td class=&quot;text-center&quot;&gt;
                        &lt;div class=&quot;dropdown&quot;&gt;
                          &lt;button data-id=&quot;{{ $result-&gt;id }}&quot; data-idtipebelanja=&quot;{{ $result-&gt;tipebelanja }}&quot; data-idtipebelanjadetail=&quot;{{ $result-&gt;tipebelanjadetail }}&quot; data-toggle=&quot;modal&quot; data-target=&quot;#editspj&quot; class=&quot;btn btn-success btn-s&quot; style=&quot;background-color:#138496;border-color:#138496&quot;&gt;&lt;i class=&quot;fad fa-pencil&quot;&gt;&lt;/i&gt;&lt;/button&gt;
                          &lt;button data-id=&quot;{{ $result-&gt;id }}&quot; data-toggle=&quot;modal&quot; data-target=&quot;#deletespj&quot; class=&quot;btn btn-success btn-s&quot; style=&quot;background-color:#eb1c0f;border-color:#eb1c0f&quot;&gt;&lt;i class=&quot;fad fa-trash&quot;&gt;&lt;/i&gt;&lt;/button&gt;
                          &lt;button type=&quot;button&quot; data-toggle=&quot;dropdown&quot; class=&quot;btn btn-warning dropdown dropdown-toggle&quot; aria-haspopup=&quot;true&quot; aria-expanded=&quot;false&quot;&gt;&lt;i class=&quot;fad fa-info&quot;&gt;&lt;/i&gt;&lt;/button&gt;
                          &lt;div class=&quot;dropdown-menu&quot;&gt;
                                  &lt;a class=&quot;dropdown-item&quot; href=&quot;{{ Route('spj.detail', $result-&gt;id)}}&quot;&gt;&lt;i style=&quot;padding-right:5px&quot; class=&quot;fad fa-eye&quot;&gt;&lt;/i&gt;Detail&lt;/a&gt;
                          &lt;/div&gt;
                        &lt;/div&gt;
                        &lt;/td&gt;
                      &lt;/tr&gt;
                        @empty
                        &lt;tr&gt;
                          &lt;td colspan=&quot;7&quot; class=&quot;text-center&quot;&gt;Tidak ada data &lt;i class=&quot;far fa-frown fa-5&quot;&gt;&lt;/i&gt;&lt;/td&gt;
                        &lt;/tr&gt;
  @endforelse
  </code></pre>