Laravel将变量从控制器中的1个函数传递给另一个函数

时间:2016-07-13 19:02:08

标签: php laravel controller

我在控制器中有2个功能

这是get函数

public function getAllDept($allDEPT2)
{
    $allDEPT = Facultyform::where('Home_Department_Desc', '=', $allDEPT2)->paginate(1);

 $allDEPTs =  View::make('faculty/dept', compact('allDEPT','certifications','certifications1','advisment','facultydata','facultydata1','facultydegree','facultydegree1','commembership', 'commembership1','otherpro','otherpro1','priorteaching','priorteaching1','publication','publication1','professioncourse','professioncourse1','support','support1','otherschool','otherschool1' ));



    return $allDEPTs;
}

这是帖子功能

 public function getAllDept2()
 {
  if ( Session::token() !== Input::get( '_token' ) ) {
            return Response::json( array(
                'msg' => 'Unauthorized attempt to create setting'
            ) );
        }

        $fId = Input::get( 'faculty_id' );

        $faculty = Facultyform::where('File_Number', '=', $fId)
        ->get();
        if($faculty[0]['isChair'] == 'Y'){
    $allDEPT2 = $faculty[0]['Home_Department_Desc'];

    $allDEPT = Facultyform::where('Home_Department_Desc', '=', $allDEPT2)->paginate(1);
    $this->getAllDept($allDEPT2);

      $allDEPTs =  View::make('faculty/dept', compact('allDEPT','certifications','certifications1','advisment','facultydata','facultydata1','facultydegree','facultydegree1','commembership', 'commembership1','otherpro','otherpro1','priorteaching','priorteaching1','publication','publication1','professioncourse','professioncourse1','support','support1','otherschool','otherschool1' ));



    return $allDEPTs;
    }

我创建了两条路线

 Route::get('faculty/dept',array(
    'uses' => 'FacultyController@getAllDept'
 )); 

Route::post('faculty/dept', array(
    'as' => 'faculty/dept',
    'uses' => 'FacultyController@getAllDept2'
));

当最终用户使用faculty / getDept并输入id时,它会使用视图显示列表中的第1项,但是当我点击下一页时出现以下错误 缺少FacultyController :: getAllDept()的参数1 如果我对一个部门进行硬编码,那么分页就会起作用,但它仅适用于硬编码部门。

我试过$ this-> getALLDept($ allDEPT2); 我尝试创建变量的会话 我试图让变量成为全局变量 我可以在控制器中做一些事情,将变量从一个函数传递给另一个函数,还是在路径中执行此操作?

这就是其中一个函数的样子

      public function getFacultyId() {
        //check if its our form
        if ( Session::token() !== Input::get( '_token' ) ) {
            return Response::json( array(
                'msg' => 'Unauthorized attempt to create setting'
            ) );
        }
       //check if its our form
        if ( Session::token() !== Input::get( '_token' ) ) {
            return Response::json( array(
                'msg' => 'Unauthorized attempt to create setting'
            ) );
        }

        $fId = Input::get( 'faculty_id' );

        $faculty = Facultyform::where('File_Number', '=', $fId)
        ->get();
        $lastNames = $faculty[0]['Last_Name'];
        $firstNames = $faculty[0]['First_Name'];

            $certCount = Certifications::where('Last_Name','=',$lastNames)
        ->where('First_Name','=',$firstNames)

        ->count();
        if ($certCount < 1)
        {
        $certifications1 = "N/A";
        }
        else
        {
        $certifications1 = " ";
        }

        $certifications = Certifications::where('Last_Name','=',$lastNames)
        ->where('First_Name','=',$firstNames)

        ->get();

        $advisment = Advisment::where('Last_Name','=',$lastNames)           
        ->where('First_Name','=',$firstNames)
        ->count();

        $fsCount = $facultydata = Facultydata::where('Last_Name','=',$lastNames)            
        ->where('First_Name','=',$firstNames)
        ->count();
        if ($fsCount < 1)
        {
        $facultydata1 = "N/A";
        }
        else
        {
        $facultydata1 = " ";
        }
        $facultydata = Facultydata::where('Last_Name','=',$lastNames)           
        ->where('First_Name','=',$firstNames)
        ->get();

        $eduCount =  Facultydegree::where('Last_Name','=',$lastNames)           
        ->where('First_Name','=',$firstNames)
        ->count();
        if ($eduCount < 1)
        {
        $facultydegree1 = "N/A";
        }
        else
        {
        $facultydegree1 = " ";
        }
        $facultydegree = Facultydegree::where('Last_Name','=',$lastNames)           
        ->where('First_Name','=',$firstNames)
        ->get();

        $comCount = Commembership::where('Last_Name','=',$lastNames)            
        ->where('First_Name','=',$firstNames)
        ->count();
        if ($comCount < 1)
        {
        $commembership1 = "N/A";
        }
        else
        {
        $commembership1 = " ";
        }

        $commembership = Commembership::where('Last_Name','=',$lastNames)           
        ->where('First_Name','=',$firstNames)
        ->get();

        $otherCount = OtherPro::where('Last_Name', '=', $lastNames)
        ->where('First_Name','=',$firstNames)
        ->count();
        if ($otherCount < 1)
        {
        $otherpro1 = "N/A";
        }
        else
        {
        $otherpro1 = " ";
        }
        $otherpro = OtherPro::where('Last_Name', '=', $lastNames)
        ->where('First_Name','=',$firstNames)
        ->get();

        $ptCount = PriorTeaching::where('Last_Name','=',$lastNames)         
        ->where('First_Name','=',$firstNames)
        ->count();
        if ($ptCount < 1)
        {
        $priorteaching1 = "N/A";
        }
        else
        {
        $priorteaching1 = " ";
        }
        $priorteaching = PriorTeaching::where('Last_Name','=',$lastNames)           
        ->where('First_Name','=',$firstNames)
        ->get();

        $pubCount = Publications::where('Last_Name','=',$lastNames)         
        ->where('First_Name','=',$firstNames)
        ->count();
        if ($pubCount < 1)
        {
        $publication1 = "N/A";
        }
        else
        {
        $publication1 = " ";
        }
        $publication = Publications::where('Last_Name','=',$lastNames)          
        ->where('First_Name','=',$firstNames)
        ->get();
        $pcCount = ProfessionCourse::where('Last_Name','=',$lastNames)          
        ->where('First_Name','=',$firstNames)
        ->count();
            if ($pcCount < 1)
        {
        $professioncourse1 = "N/A";
        }
        else
        {
        $professioncourse1 = " ";
        }
        $professioncourse = ProfessionCourse::where('Last_Name','=',$lastNames)         
        ->where('First_Name','=',$firstNames)
        ->get();
        $supportCount = Support::where('Last_Name','=',$lastNames)          
        ->where('First_Name','=',$firstNames)
        ->count();
            if ($supportCount < 1)
        {
        $support1 = "N/A";
        }
        else
        {
        $support1 = " ";
        }
        $support = Support::where('Last_Name','=',$lastNames)           
        ->where('First_Name','=',$firstNames)
        ->get();

        $osCount = OtherSchool::where('Last_Name','=',$lastNames)           
        ->where('First_Name','=',$firstNames)
        ->count();
        if ($osCount < 1)
        {
        $otherschool1 = "N/A";
        }
        else
        {
        $otherschool1 = " ";
        }
        $otherschool = OtherSchool::where('Last_Name','=',$lastNames)           
        ->where('First_Name','=',$firstNames)
        ->get();
$allviews =  View::make('faculty.faculty', compact('faculty','certifications','certifications1','advisment','facultydata','facultydata1','facultydegree','facultydegree1','commembership', 'commembership1','otherpro','otherpro1','priorteaching','priorteaching1','publication','publication1','professioncourse','professioncourse1','support','support1','otherschool','otherschool1'));

我为每个要求它的部门制作一个。到目前为止,我有3个(所有的,财务的和学术的)然后它让我做出一个,每个部门主席只能通过输入他们的凭证来查看他们的部门信息。安全工作只是想将他们的部门传递给另一个部门

我在名为department

的控制器中创建了一个变量
public function getAllDept($department){

$department = Facultyform::where('Home_Department_Desc', '=',$department)->get();
echo($department[0]['Home_Department_Desc']);
$allDEPT = Facultyform::where('Home_Department_Desc', '=',$department[0]['Home_Department_Desc'])->paginate(1);
$lastNames = $allDEPT[0]['Last_Name'];
$firstNames = $allDEPT[0]['First_Name'];

$certCount = Certifications::where('Last_Name','=',$lastNames)
->where('First_Name','=',$firstNames)

->count();
if ($certCount < 1)
{
$certifications1 = "N/A";
}
else
{
$certifications1 = " ";
}

$certifications = Certifications::where('Last_Name','=',$lastNames)
->where('First_Name','=',$firstNames)

->get();

$advisment = Advisment::where('Last_Name','=',$lastNames)           
->where('First_Name','=',$firstNames)
->count();

$fsCount = $facultydata = Facultydata::where('Last_Name','=',$lastNames)            
->where('First_Name','=',$firstNames)
->count();
if ($fsCount < 1)
{
$facultydata1 = "N/A";
}
else
{
$facultydata1 = " ";
}
$facultydata = Facultydata::where('Last_Name','=',$lastNames)           
->where('First_Name','=',$firstNames)
->get();

$eduCount =  Facultydegree::where('Last_Name','=',$lastNames)           
->where('First_Name','=',$firstNames)
->count();
if ($eduCount < 1)
{
$facultydegree1 = "N/A";
}
else
{
$facultydegree1 = " ";
}
$facultydegree = Facultydegree::where('Last_Name','=',$lastNames)           
->where('First_Name','=',$firstNames)
->get();

$comCount = Commembership::where('Last_Name','=',$lastNames)            
->where('First_Name','=',$firstNames)
->count();
if ($comCount < 1)
{
$commembership1 = "N/A";
}
else
{
$commembership1 = " ";
}

$commembership = Commembership::where('Last_Name','=',$lastNames)           
->where('First_Name','=',$firstNames)
->get();

$otherCount = OtherPro::where('Last_Name', '=', $lastNames)
->where('First_Name','=',$firstNames)
->count();
if ($otherCount < 1)
{
$otherpro1 = "N/A";
}
else
{
$otherpro1 = " ";
}
$otherpro = OtherPro::where('Last_Name', '=', $lastNames)
->where('First_Name','=',$firstNames)
->get();

$ptCount = PriorTeaching::where('Last_Name','=',$lastNames)         
->where('First_Name','=',$firstNames)
->count();
if ($ptCount < 1)
{
$priorteaching1 = "N/A";
}
else
{
$priorteaching1 = " ";
}
$priorteaching = PriorTeaching::where('Last_Name','=',$lastNames)           
->where('First_Name','=',$firstNames)
->get();

$pubCount = Publications::where('Last_Name','=',$lastNames)         
->where('First_Name','=',$firstNames)
->count();
if ($pubCount < 1)
{
$publication1 = "N/A";
}
else
{
$publication1 = " ";
}
$publication = Publications::where('Last_Name','=',$lastNames)          
->where('First_Name','=',$firstNames)
->get();
$pcCount = ProfessionCourse::where('Last_Name','=',$lastNames)          
->where('First_Name','=',$firstNames)
->count();
if ($pcCount < 1)
{
$professioncourse1 = "N/A";
}
else
{
$professioncourse1 = " ";
}
$professioncourse = ProfessionCourse::where('Last_Name','=',$lastNames)         
->where('First_Name','=',$firstNames)
->get();
$supportCount = Support::where('Last_Name','=',$lastNames)          
->where('First_Name','=',$firstNames)
->count();
if ($supportCount < 1)
{
$support1 = "N/A";
}
else
{
$support1 = " ";
}
$support = Support::where('Last_Name','=',$lastNames)           
->where('First_Name','=',$firstNames)
->get();

$osCount = OtherSchool::where('Last_Name','=',$lastNames)           
->where('First_Name','=',$firstNames)
->count();
if ($osCount < 1)
{
$otherschool1 = "N/A";
}
else
{
$otherschool1 = " ";
}
$otherschool = OtherSchool::where('Last_Name','=',$lastNames)           
->where('First_Name','=',$firstNames)
->get();
$allDEPTs =  View::make('faculty/dept', compact('allDEPT','certifications','certifications1','advisment','facultydata','facultydata1','facultydegree','facultydegree1','commembership', 'commembership1','otherpro','otherpro1','priorteaching','priorteaching1','publication','publication1','professioncourse','professioncourse1','support','support1','otherschool','otherschool1' ));



return $allDEPTs;
//$queries = DB::getQueryLog();
//$last_query = end($queries);
//var_dump($last_query);
//die();
}

在我的路线中,我将变量添加到uri

//faculty get users by department//
 Route::get('faculty/dept/{department}',
    'FacultyController@getAllDept'
    ); 

当我做教师/部门/ ACDS时,我去获取数据 这是一张照片 Working before pagination

当我点击下一步时,我收到错误
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
这是我点击下一个/ facultyform / faculty / dept?page = 2时得到的uri
也许我必须让它去/ facultyform / faculty / dept / department?page2?


以下是我得到的错误的更多信息  获取数据
关键价值 第2页 POST数据为空 文件为空
这是一个工作网址 / facultyform /教师/部门/卫生%20Science%20于是%20Technologies?页= 2
现在我只需要在网址中获取部门,我就完成了:)

1 个答案:

答案 0 :(得分:0)

当我进入视图时,我能够对部门进行硬编码并且所有工作现在我只需要将变量从控制器传递到视图

        {{ Form::open(array(
    'route' => ['faculty/dept/{department}','ACDS'],
    'method' => 'post',
    'id' => 'form-getdept-setting'
))}}

{{ Form::label( 'faculty_id', 'Faculty Id:' ) }}
{{ Form::text( 'faculty_id', '', array(
    'id' => 'faculty_id',
    'placeholder' => 'Enter Faculty Id',
    'maxlength' => 20,
    'required' => true,
) ) }}


{{ Form::submit( 'Find Information', array(
    'id' => 'btn-getdeptsetting',
) ) }}

{{ Form::close() }}


public function getAllDept2($department){
if ( Session::token() !== Input::get( '_token' ) ) {
return Response::json( array(
'msg' => 'Unauthorized attempt to create setting'
) );
}

$fId = Input::get( 'faculty_id' );

$faculty = Facultyform::where('File_Number', '=', $fId)
->get();
if($faculty[0]['isChair'] == 'Y'){
$department = Facultyform::where('Home_Department_Desc', '=',$department)->get();
$department2 = $department[0]['Home_Department_Desc'];
$allDEPT2 = $faculty[0]['Home_Department_Desc'];
dd($department);
$allDEPT = Facultyform::where('Home_Department_Desc', '=', $allDEPT2)->paginate(1);
$this->getAllDept($allDEPT2);
$lastNames = $allDEPT[0]['Last_Name'];
$firstNames = $allDEPT[0]['First_Name'];
$certCount = Certifications::where('Last_Name','=',$lastNames)
->where('First_Name','=',$firstNames)

->count();
...
$allDEPTs =  View::make('faculty/dept',compact('allDEPT','certifications','certifications1'
,'advisment','facultydata','facultydata1','facultydegree','facultydegree1','commembership', 
'commembership1','otherpro','otherpro1','priorteaching','priorteaching1','publication'
,'publication1','professioncourse','professioncourse1','support','support1','otherschool'
,'otherschool1'));
return $allDEPTs;
/* $queries = DB::getQueryLog();
$last_query = end($queries);
var_dump($last_query);
die(); */
}
else{
return Redirect::to('/');
}
}