【问题标题】:HREF sends parameters to controller and returns a ViewHREF 向控制器发送参数并返回一个视图
【发布时间】:2015-06-26 00:42:23
【问题描述】:

因此,我目前正在处理一个项目,其中有一个表格,其中显示了经理的概述以及他们分配给当前具有特定状态的应用程序的数量。我要做的是当用户单击一个数字(即计数)时,它会返回一个列表视图,该视图仅从数据库中查询分配给该经理和该状态的那些应用程序。否则,如果用户转到应用程序视图,它应该显示所有应用程序。

这是我的概览视图的代码:

    <table class="table tablesorter" id="tblAffs">
  <thead>
  <tr class="tblhead">
    <th>AM</th>
    <th>Qualified Prospect</th>
    <th>Unqualified Prospect</th>
    <th>Contacted</th>
    <th>Converted To Account</th>
    <th>Pending Integration</th>
    <th>Revenue Generating</th>
  </tr>
  </thead>
  <tbody>

  @foreach ($totalCount as $id => $name) {{-- id is the admin id --}}
    @foreach($name as $n => $status)  {{-- $n is the name, $status is array of the counts --}}
    <tr>
      <td>
        {{$n}}
        <br>
        <a href="#">Closed</a>
      </td>
          <td><a href="/ap1/{{$new_lead}}/applications?status=2">{{ isset($status[2]) ? $status[2] : 0 }}</a></td>
          <td><a href="/ap1/{{$new_lead}}/applications?status=1">{{ isset($status[1]) ? $status[1] : 0 }}</a></td>
          <td><a href="/ap1/{{$new_lead}}/applications?status=3">{{ isset($status[3]) ? $status[3] : 0 }}</a></td>
          <td><a href="/ap1/{{$new_lead}}/applications?status=4">{{ isset($status[4]) ? $status[4] : 0 }}</a></td>
          <td><a href="/ap1/{{$new_lead}}/applications?status=5">{{ isset($status[5]) ? $status[5] : 0 }}</a></td>
          <td><a href="/ap1/{{$new_lead}}/applications?status=6">{{ isset($status[6]) ? $status[6] : 0 }}</a></td>
    </tr>
  @endforeach
  @endforeach



  </tbody>
</table>

在我的控制器上,这是概述的代码和数据结构的创建方式:

     public function overview()
  {
    $query= DB::table('admins')
      ->join('advertiser_applications', 'admins.id', '=', 'advertiser_applications.assigned_to')
      ->selectRaw('advertiser_applications.status, admins.id, admins.first_name, COUNT(advertiser_applications.status) count')
      ->groupBy('admins.id', 'advertiser_applications.status');

    $counts = $query->get();
    $totalCount = [];
    foreach($counts as $count){
      $totalCount[$count->id][$count->first_name][$count->status] = $count->count;
    }
    return View::make('admin.crm.overview', ['totalCount' => $totalCount, 'admin_id' => AuthAdmin::admin(false), 'tpl_title' => 'CRM Advertiser Overview', 'new_lead' => 'advertisers']);

  }

这是我的控制器中用于显示列表的视图的代码。这是我很困惑的地方,如果单击该 href,我将如何传递该状态和 id。

   public function index($param)
  {
    $query = AdvertiserApplication::with('admin');
    $status = Input::get('status');

    if (!empty($status) || $status === '0')
      $query->where('status', '=', $status);

    $applications = $query->get();

    return View::make('admin.advertisers.application_list', ['applications' => $applications, 'admin_id' => AuthAdmin::admin(false)]);
  }

现在我不确定这是否是我在 href 中编写的链接的正确方法。我应该做一个 URL 链接来路由或类似的东西吗?我还不确定。任何提示将不胜感激!

【问题讨论】:

    标签: php mysql laravel model-view-controller


    【解决方案1】:

    有了这个你的网址:

     <a href="/ap1/{{$new_lead}}/applications?status=2">{{ isset($status[2]) ? $status[2] : 0 }}</a>
    

    如果你有

    route::get('/ap1/{new_lead}', array('as' => 'page-name', 'uses' => 'yourController@functionName'));
    

    在控制器中:

    public function functionName($new_lead){
     // use $new_lead here to get your data
    }
    

    【讨论】:

    • 好吧,这是有道理的,但问题是 new_lead 是一些其他变量,它只是告诉我是广告商还是卖家。我要发送的是 $totalCount 数组中的 $id (您在第一个 foreach 语句中看到)和该状态号。状态[1] 或状态[2] 等
    • 现在,如果您要传递 ID,那就太好了。只需将 $new_lead 替换为 $id 并在您的控制器中获取 $id ,根据该 $id 查询数据库并查看它是广告商还是卖家。我相信 $id 是独一无二的。
    • 它不是,但这是有道理的,我可以同时传递状态和id?还是可以在 get 中将状态保留为硬编码并仅将 id 发送给控制器并且控制器接受 id?
    • 还有 Michel,你能解释一下 route::get 部分吗?我是否将其放在 href 而不是 "/ap1/{{$new_lead...?
    • @Milan AM 值是传入href,路由只传参数给控制器一个例子在这个Route::get('/page/ {ID}', ....); Controller public function page($id) 这里$id会返回2。
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 2019-07-26
    • 2011-07-03
    • 2012-07-15
    • 2020-08-18
    • 2012-06-05
    相关资源
    最近更新 更多