问题
I have just started using laravel and currently using the version 5.2. I am using two forms to get the data. First form in first view and second form in second view. I need to figure out a way to tell the second form, the form id of the first one, which the second form will be linked to.
I know this can be done by passing the values using the URL. But I lack the knowledge of the correct syntax.
1- How to send data while redirecting?
2- How should the route look like?
3- How to access that value in the second view, in order to pass that value when the second form submits?
I have googled a lot about this but couldn't understand those advanced syntax. Any help is appreciated.
Thanks in advance.
This is the code in controller:
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
Session::flash('values',$request->azauj_id);
return redirect('/add/requirement');
}
public function getCreateRequirement(Request $request){
$att = Session::get('value');
Session::flash('value',$att);
return view('req');
}
public function postCreateRequirement(Request $request){
dd(Session::get('value'));
}
The forms are plain html forms with post methods of submission
When I use dd(Session::get('value'));
, i get null. It means that the value is not being passed. To the postCreateRequirement method which is called when the second form is submitted.
Below are the routes.
//For Add Profile Page
Route::get('/add', 'ProfileController@getCreateProfile');
//For Add Profile Form Submission
Route::post('/add', 'ProfileController@postCreateProfile');
//For Add Requirements Page
Route::get('/add/requirement', 'ProfileController@getCreateRequirement');
//For Add Requirements Form Submission
Route::post('/add/requirement', 'ProfileController@postCreateRequirement');
回答1:
1- How to send data while redirecting?
You can simply pass data with your redirect using the ->with()
method which creates an session that will only appear on the next page more here
Example: Say you want to send a status down to your view you add the with to your redirect:
// Where you are redirecting to
redirect("/otherRoute")->with("status", "Profile updated!");
// session name // data
Now you simply check if the session exist and echo it out:
// If the session does not exist it will return false and not create it
@if (session("status"))
<div class="alert alert-success">
// echo out the session
{{ session("status") }}
</div>
@endif
2- How should the route look like?
Routes should be defined in the routes.php file located in the http directory assuming you are posting the data you should make the routes and connect them to your controller like so:
//For Add Profile Page
Route::get('/add', 'ProfileController@getCreateProfile');
//For Add Profile Form Submission
Route::post('/add', 'ProfileController@postCreateProfile');
//For Add Requirements Page
Route::get('/add/requirement', 'ProfileController@getCreateRequirement');
//For Add Requirements Form Submission
Route::post('/add/requirement', 'ProfileController@postCreateRequirement');
3- How to access that value in the second view, in order to pass that value when the second form submits?
You could simply use the ->with()
method in your redirect
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
return redirect('/add/requirement')->with("value",$request->azauj_id);
}
Get the value
public function getCreateRequirement(Request $request){
$value = session("value");
// compact it and trow it in an input to pass it trough to the last controller method
return view('req');
}
public function postCreateRequirement(Request $request){
$request->get("value");
}
OR Create a global session and flush it afterwards
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
session("value",$request->azauj_id);
return redirect('/add/requirement');
}
Get the value
public function getCreateRequirement(Request $request){
return view('req');
}
public function postCreateRequirement(Request $request){
$value = session("value");
$request->session()->forget("value");
}
Note: Laravel does not flush sessions when somone logs out these will remain if not flushed by hand or using the method $request->session()->flush();
来源:https://stackoverflow.com/questions/34492537/pass-value-from-one-view-to-other-laravel-routes-configuration