简体   繁体   中英

How to pass variables into different functions inside a php controller

I`m trying to create a form-select field displaying all of the schools with a specified type. The laravel functionality of the code works. As I had this on one page and it all worked fine. But I have been asked to move them on to two separate pages. And my question is how do I get my $type variable from my storeType function into my school Selection function properly? See Code:

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

Routes:

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');

if both the functions are in same controller than you can make type as global variable.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM