【问题标题】:Laravel pass variable from 1 function in controller to another oneLaravel 将变量从控制器中的 1 个函数传递到另一个函数
【发布时间】:2016-11-16 12:23:36
【问题描述】:

我在一个控制器中有 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 时,它会显示列表中的第一项使用视图,但是当我单击下一页时出现以下错误 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 个(全部,财务和学术)然后我想制作一个,每个系主任可以通过输入他们的凭据来查看他们的部门信息。安全工作只是想将他们的部门传递给其他职能

我在控制器中创建了一个变量,叫做部门

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 时,我会去获取数据 这是它的照片

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


这是我收到的错误的更多信息
获取数据
关键价值
第 2 页
POST 数据为空
文件为空
这是一个有效的网址
/facultyform/faculty/dept/Health%20Science%20and%20Technologies?page=2
现在我只需要在 url 中获取部门,我就完成了:)

【问题讨论】:

  • 你从哪里得到faculty_id?它不包含在路线中。您还有其他传递 id 的 get 路线吗?
  • 我创建了一个路由 Route::post('/', array( 'as' => 'faculty.create', 'uses' => 'FacultyController@getID' ));然后使用获取 id 的表单
  • 流程和组织有很多问题......您正在对所有内容进行硬编码。
  • 这不是每个函数的完整代码,每个函数都有超过 200 行代码,只是想展示我的函数的概念
  • 你使用的是哪个 laravel 版本?当 laravel 为您提供金盘时,您似乎让事情变得更难了。

标签: php laravel controller


【解决方案1】:

当我转到视图时,我可以对部门进行硬编码并且一切正常现在我只需要将变量从控制器传递到视图

        {{ 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('/');
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多