簡體   English   中英

How to pass variables into different functions inside a php controller

[英]How to pass variables into different functions inside a php controller

我正在嘗試創建一個表單選擇字段,顯示所有具有指定類型的學校。 代碼的 laravel 功能有效。 因為我在一頁上有這個,所以一切正常。 但我被要求將它們移到兩個單獨的頁面上。 我的問題是如何將我的 $type 變量從我的 storeType function 正確地放入我的學校選擇 function 中? 見代碼:

public function schoolSelection(School $school,$type)
{
    $schools=$school->where('type','=',$type)->get();
    return view('auth.schoolSelection',compact('schools'));
}

public function storeType(Request $request, School $school)
{
    $data=$request->all();
    $type=$data['schoolType'];

    return redirect()->route('schoolSelection',compact('type'));
}

路線:

Route::get('schoolSelection/type',[RegisteredUserController::class,'schoolType'])->name('schoolType');

Route::get('schoolSelection/school',[RegisteredUserController::class,'schoolSelection'])->name('schoolSelection');

Route::post('schoolSelection/type',[RegisteredUserController::class,'storeType'])->name('type.store');

Route::post('schoolSelection/school',[RegisteredUserController::class,'schoolSelection'])->name('schoolSelection.store');

如果這兩個函數都在同一個 controller 中,那么您可以將類型設置為全局變量。

class TestController extends Controller
{
    private $type;

   public function storeType(Request $request, School $school)
{
    $data=$request->all();
    $this->type=$data['schoolType'];

    return redirect()->route('schoolSelection',compact('type'));
}
   public function schoolSelection(School $school)
{
   $type = $this->$type;

    $schools=$school->where('type','=',$type)->get();
    return view('auth.schoolSelection',compact('schools'));
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM