I have successfully displayed data from one table in one part of my application, but the data won't display in another part. I want to include the Record for the winning team of that year. My View returns nothing when it should return an integer. I am using laravel 5.6.
Controller
$seasonOneWins=gameData::where('homeTeam', '81-North Stars')
->where('homeWin', '1')
->where('year', '2016');
View:
@if($seasonOneWins)
@foreach($seasonOneWins as $seasonOneWin)
<p>{!!$seasonOneWin!!}<p>
@endforeach
@endif
I can't get anything to display in between the foreach's. I assume this means that my formula is suggesting that there is nothing that meets those requirements when I know there is.
So this was a misunderstanding. I kept trying to use count() on a foreach, which caused an error. I had the wrong syntax in my view, instead of the foreach() I should have coded:
View
<p> {!!$seasonOneWins!!}<p>
Controller
$seasonOneWins=GameData::where('team', '81 North Stars')
->where('win', '1')
->where('year', 2016)
->count();
Use get()
method in your query to return a collection like this
$seasonOneWins = gameData::where('homeTeam', '81-North Stars')
->where('homeWin', '1')
->where('year', '2016')
->get();
After that you can loop to the collection return by your query.
Read the documentation here.