简体   繁体   中英

Laravel: Calling method of a controller class from blade template

How can I call a method of a controller from blade template and receive the data is returned by the method. For Example:

MeetingRoomController

class MeetingRoomController extends \BaseController
{

    public function index()
    {
        //get all the meeting room
        $meetingRoomCountry = MeetingRoom::select('country')->distinct()->orderBy('country')->get();
        return View::make('index')->with('meetingroom', $meetingRoomCountry);
    }

    public function findLocationForNavBar( $country )
    {
        $meetingRoomLocation = MeetingRoom::select('location')
                ->distinct()
                ->where('country', '$country')
                ->orderBy('country')
                ->get();
        return View::make('index')- >with('meetingroomLocation', $meetingRoomLocation);
    }

}

View - index.blade.php

<div id="dropdown-lvl1" class="panel-collapse collapse">
    <div class="panel-body">
         <ul class="nav navbar-nav">
         @foreach($meetingroom as $key => $value)                                            
             <li class="panel panel-default" id="dropdown">
                 <a data-toggle="collapse" href="#dropdown-lvl2">
                     <span class="glyphicon glyphicon-off"></span> {{$country = $value->country}} <span class="caret"></span>
                  </a>
                  <div id="dropdown-lvl2" class="panel-collapse collapse">
                      <div class="panel-body">
                          <ul class="nav navbar-nav">
                              <li><a href="#">Location</a></li>            
                          </ul>
                      </div>
                  </div>
             </li>
             @endforeach
         </ul>
     </div>
 </div>

I want to collect the location based on corresponding country. According to my logic, First of all I collected the distinct country then searched the location by findLocationForNavBar method in MeetingRoomController.

If you're using Laravel 5.1, you could try a Service Injection from your blade template.

http://laravel.com/docs/5.1/blade#service-injection

Example from the docs:

@inject('metrics', 'App\Services\MetricsService')

<div>
    Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>

您可以在index()方法中完成所有这些工作,然后发送位置和国家/地区进行查看。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM