【问题标题】:Laravel 5 Route ErrorLaravel 5 路由错误
【发布时间】:2017-10-02 11:41:29
【问题描述】:

我是 laravel 5.4 的新手,所以我需要在一个视图中调用这两个函数。 这是 2 个控制器。

 public function index()
{
   $items = trainingprogramedetails::all()->last();
     return view('programesession/index',compact('items'));

}

 public function list()
{
   $lists = trainingprogramedetails::all();
     return view('programesession/index',compact('lists'));

}

这是我的观点 index.blade.php

<thead>
      <th>Name</th>
      <th>Data</th>
          </thead>

          <tbody>
              <tr>
                <td>Date</td>
                <td>{{ $items->date_of_programe }}</td>
              </tr>

              <tr>
                <td>Time</td>
                <td>{{ $items->time }}</td>
              </tr>

              <tr>
                <td>Venue</td>
                <td>{{ $items->venue }}</td>
              </tr>
          </tbody>
         </table> 

             <table class="table table-striped">
              <thead>

                <th>Trainee Programe ID</th>
                <th>Presenter Name</th>
                <th>Division</th>
                <th>Date</th>
              </thead>

              <tbody>
              @foreach($lists as $item)

                 <tr>

              <td>{{ $item->training_programe_id }}</td>
              <td>{{ $item->presenter_name }}</td>
              <td>{{ $item->division }}</td>
              <td>{{ $item->date }}</td>
          </tr>
                @endforeach

              </tbody>
      </table> 

所以这是错误到达这里。

这是我的路

Route::group(['middleware' => ['web']], function () {
Route::resource('/programelist', 'TrainingProgrameSeassion');

});

你能帮忙解决这个问题吗?

【问题讨论】:

  • 为什么你认为这是路线的问题?您甚至阅读了错误消息吗?
  • 我是 laravel 的新手。不管这是我的本科项目的情况,我都必须使用 laravel 来做。我认为这是路线问题。如果我知道,我不会问它问题

标签: php laravel laravel-5 view routes


【解决方案1】:

你应该像这样使用正确的代码

    public function index()
{
   $items = trainingprogramedetails::all()->last();

return view('programesession.index')->with(compact('items'));

}

 public function list()
{
   $lists = trainingprogramedetails::all();

return view('programesession.index')->with(compact('lists'));

}

【讨论】:

    【解决方案2】:

    有什么理由不将这两个功能结合起来?另一个解决方案应该处理当前的错误。如果确实需要两个函数,最好也使用部分函数

    public function index()
    {
       $items = trainingprogramedetails::all()->last();
       $lists = trainingprogramedetails::all();
       return view('programesession/index',compact('lists','items'));
    }
    

    【讨论】:

      【解决方案3】:

      正如问题中所述,我知道您在调用索引路由时可能正在调用index() 函数而不是list() 函数。

      这个问题不太清楚,需要进一步澄清。

      您需要在单个函数中同时使用这两个变量来实现解决方案。

      public function index()
      {
          $items = trainingprogramedetails::all()->last();
          $lists = trainingprogramedetails::all();
      
          return view('programesession/index', compact('items', 'lists'));
      }
      

      错误是因为您调用 index 方法或 list 方法而不是同时调用两者,并且只在视图中传递了 items 变量,而在视图中没有传递 lists 变量。

      如果答案不是您想要的,请添加更多代码并详细说明您的问题。

      【讨论】:

      • 很高兴它帮助了你:)
      【解决方案4】:

      我认为您需要更新您的视图,例如:

      @if(isset($items))
      <table>
      <thead>
           <th>Name</th>
           <th>Data</th>
               </thead>
      
               <tbody>
                   <tr>
                     <td>Date</td>
                     <td>{{ $items->date_of_programe }}</td>
                   </tr>
      
                   <tr>
                     <td>Time</td>
                     <td>{{ $items->time }}</td>
                   </tr>
      
                   <tr>
                     <td>Venue</td>
                     <td>{{ $items->venue }}</td>
                   </tr>
               </tbody>
              </table>
      @endif
      
      @if(isset($lists))
                  <table class="table table-striped">
                   <thead>
      
                     <th>Trainee Programe ID</th>
                     <th>Presenter Name</th>
                     <th>Division</th>
                     <th>Date</th>
                   </thead>
      
                   <tbody>
                   @foreach($lists as $item)
      
                      <tr>
      
                   <td>{{ $item->training_programe_id }}</td>
                   <td>{{ $item->presenter_name }}</td>
                   <td>{{ $item->division }}</td>
                   <td>{{ $item->date }}</td>
               </tr>
                     @endforeach
      
                   </tbody>
           </table>
      @endif
      

      希望这对你有用。

      【讨论】:

        猜你喜欢
        • 2016-07-01
        • 1970-01-01
        • 2013-05-14
        • 2015-06-24
        • 2016-07-23
        • 2015-07-19
        • 1970-01-01
        • 1970-01-01
        • 2015-09-25
        相关资源
        最近更新 更多