Donald Donald
Donald Donald

Reputation: 67

Foreach only return first value

Not sure why it is not getting all the value In my view

<?php $sum_selfmark =0?>
  @foreach
      ($criteria_criteriamarks as $criteria_criteriamark)

      SelfMark:{{$criteria_criteriamark->selfmark}}
      <?php $sum_selfmark+= $criteria_criteriamark->selfmark ?>

  @endforeach
      <p>Total:{{$sum_selfmark}}</p>

In my controller

 public function go_to_self_marking($id){
    $criteria=Criteria::find($id);
    $criteria_criteriamarks =$criteria->criteriamarks;
    return view('criterias/self-marking')
    ->with('criteria_criteriamarks',$criteria_criteriamarks);
    }

Upvotes: 3

Views: 273

Answers (2)

MONSTEEEER
MONSTEEEER

Reputation: 580

You are trying to for each a 1 row. Because $criteria=Criteria::find($id); returns only 1 row.

If you want to for each then use $criteria=Criteria::all(); It will return all the row in your Criterias Table.

Upvotes: 2

kissartisan
kissartisan

Reputation: 155

First and foremost, did you try to die and dump (dd) the $criteria->criteriamarks? So that we can check what's going on with your loop.

Seems like the @foreach loop has no issue.

Upvotes: 0

Related Questions