繁体   English   中英

Laravel:从刀片模板调用控制器类的方法

[英]Laravel: Calling method of a controller class from blade template

如何从刀片模板调用控制器的方法并接收该方法返回的数据。 例如:

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);
    }

}

查看-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>

我想根据相应的国家/地区收集位置。 根据我的逻辑,首先,我收集了不同的国家,然后通过MeetingRoomController中的findLocationForNavBar方法搜索了位置。

如果您使用的是Laravel 5.1,则可以尝试从刀片服务器模板进行服务注入

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

来自文档的示例:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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