【问题标题】:Can I reference two functions in routes for a single blade in Laravel 5.2?我可以在 Laravel 5.2 中为单个刀片引用路由中的两个函数吗?
【发布时间】:2017-10-05 23:11:02
【问题描述】:

是否可以在通往我的视图的路线中从我的控制器中引用两个函数。这是我所拥有的:

我的路线

Route::get('/list-of-staffs', ['as' => 'listofstaffs', 'uses' => 'User\MasterlistController@listofstaffsTable']);
Route::get('/list-of-staffs', ['as' => 'listofstaffs', 'uses' => 'User\MasterlistController@summaryOfstaffsTable']);

有没有合适的方法来做到这一点?


@Rodrane,这是我的代码:

控制器:
公共函数 listofstaffsTable() {

    $staffs = DB::table('staff_profiles')
        ->select('id','last_name','first_name','middle_name','name_ext','birthday','encoded_by')
        ->orderBy('last_name','ASC')            
        ->get();            

    $count = $staffs->count();

    $searchresults = ""; // define the searchresults variable       

    $i = 1;     

    foreach( $staffs as $key => $profile ) {

        $category = GetCategory::show($profile->id);
        $getcurrentklc = staffshipHistory::where('profile_id', '=', $profile->id)
                            ->orderBy('effective_date', 'DESC')                                                         
                            ->first(); // get the current klc from the most recent staffship history entry

                if( $getcurrentklc == null )
                {
                    $currentklc = "";
                }
                else
                {
                    $currentklc = $getcurrentklc->congregation_name;
                }       


        $getmemtype = staffshipHistory::where('profile_id', '=', $profile->id)
                            ->orderBy('effective_date', 'DESC')                                                         
                            ->first(); // get the current klc from the most recent staffship history entry

                if( $getmemtype == null )
                {
                    $memtype = "";
                }
                else
                {
                    $memtype = $getmemtype->staffship_type;
                }

        $getdatebaptized = staffshipHistory::where('profile_id', '=', $profile->id)
                            ->where('log_reason', '=', 'New staff')
                            ->orderBy('effective_date', 'DESC')                                                         
                            ->first(); // get the current klc from the most recent staffship history entry

                if( $getdatebaptized == null )
                {
                    $datebaptized = "";
                }
                else
                {
                    $datebaptized = $getdatebaptized->effective_date;
                }

        $getmotherinspirit1 = staffshipHistory::where('profile_id', '=', $profile->id)                              
                            ->where('log_reason', '=', 'New staff')
                            ->orderBy('effective_date', 'DESC')                                                         
                            ->first(); // get the current klc from the most recent staffship history entry

                if( $getmotherinspirit1 == null )
                {
                    $motherinspirit1 = "";
                }
                else
                {
                    $motherinspirit1 = $getmotherinspirit1->mother_spirit_1_name;
                }

        $getmemstatus = staffshipHistory::where('profile_id', '=', $profile->id)
                            ->orderBy('effective_date', 'DESC')                                                         
                            ->first(); // get the current klc from the most recent staffship history entry

                if( $getmemstatus == null )
                {
                    $memstatus = "";
                }
                else
                {
                    $memstatus = $getmemstatus->staffship_status;
                }           

        $searchresults .= '<tr>'.                                                                                      
                           '<td>'. $i .'</td>'.
                           '<td><a href="'. URL::route('viewstaffprofile', $profile->id) .'">'. $profile->last_name .' '. $profile->first_name .' '. $profile->middle_name .' '. $profile->name_ext .' </a></td>'.                                 
                           '<td>'. $currentklc .'</td>'.
                           '<td>'. $category .'</td>'.
                           '<td>'. $memtype .'</td>'.
                           '<td>'. $memstatus .'</td>'.                
                           '<td>'. $datebaptized .'</td>'.                 
                           '<td>'. $motherinspirit1 .'</td>'.
                           '<td>'. $profile->encoded_by .'</td>'.
                           '</tr>';
                           $i++;
    }


    $data = [];     
    $data['searchresults'] = $searchresults;    
    $data['staffCount'] = $count;

    return view( 'user/masterlist/list-of-staffs', $data);  

}

这是我的刀

        {!! $staffCount !!}                                 
        <hr>
        <div class="page-header" style="text-align:center;">
          <h3 class="pageHeader">
            List of staffs
            <br>
          </h3>
        </div>          
        <div class="row">           
            <table class="table table-bordered" style="text-align: left;">
                    <tr>                            
                        <th></th>
                        <th>Full Name (Last, First, Middle)</th>                            
                        <th>KLC</th>
                        <th>Category</th>
                        <th>Mem Type</th>
                        <th>Mem Status</th>                         
                        <th>Date Baptized</th>
                        <th>Mother in Spirit</th>
                        <th>Encoder</th>
                    </tr>
                    <tbody>
                        {!! $searchresults !!}
                    </tbody>
            </table>
        </div>  

请在下面查看我的文件。 这工作正常:

{!! $搜索结果!!}

但这给了我错误“调用数组上的成员函数 count()”

{!! $staffCount !!}

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

【问题讨论】:

  • 我能问一下你为什么这样做吗?
  • @Rodrane 当然,实际上,我很难尝试在单个刀片中使用多个变量,这就是我想创建另一个刀片或视图的原因。我知道那很愚蠢。也许你可以帮助我。让我在上面发布我的代码
  • @Rodrane 我已经更新了上面的帖子
  • 你在传递数据方面没有问题我认为你没有正确的数据可能在这条线上$count = $staffs-&gt;count();

标签: controller routes laravel-5.2 blade


【解决方案1】:

已经知道了。看起来像我错过的代码

【讨论】:

  • 我强烈建议您详细说明您遗漏了哪些代码以及您为解决问题所做的工作。否则,这个答案对这篇文章的未来读者来说并不是真的有用。谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-09-13
  • 2016-12-24
  • 2014-05-03
  • 2018-10-31
  • 2021-02-08
  • 2018-07-07
  • 1970-01-01
  • 2019-11-07
相关资源
最近更新 更多