Im new to laravel and studying it by creating some test projects myself in laravel 5.2. But i got stuck now with some issue in fetching data correctly from db in laravel 5.2 and display the result. I have a menu table in my db with fields -> id, menutype, itemname, itemprice, itemimage with some datas in it. I want to display it on my webpage in a specific way like as seen on the below given screenshot.
See:
And this one is my db table screenshot with the values on it. See:
i added the below codes in my Controller (GuestController.php)
public function menu() {
$result=DB::table('menu')->select('menutype')->distinct()->get();
return view('guest.menu')->with('data',$result);
}
and in the View (menu.blade.php), i had given the below code:
<div class="row">
@foreach($data as $row)
<div class="col-1-3">
<div class="wrap-col">
<h3>{{$row->menutype}}</h3>
<?php
$item=DB::table('menu')->where('menutype', $row->menutype)->get();
?>
@foreach($item as $row)
<div class="post">
<a href="#"><img src="assets/images/{{$row->itemimage}}"/></a>
<div class="wrapper">
<h5><a href="#">{{$row->itemname}}</a></h5>
<span>Rs.{{$row->itemprice}}/-</span>
</div>
</div>
@endforeach
</div>
</div>
@endforeach
</div>
This works perfectly and i am getting the desired output as seen on the products page screenshot given above. But i know this method is not correct, because i am giving the query statement on the View itself like as given below to fetch data and its against the MVC concept:
<?php $item=DB::table('menu')->where('menutype', $row->menutype)->get(); ?>
So is there any other simple and better way i can implement to get the above said desired output along with keeping the MVC Standards?? Please help! Thanks in advance...