将值从一个视图传递到其他laravel路由配置 [英] Pass value from one view to other laravel routes configuration

查看:115
本文介绍了将值从一个视图传递到其他laravel路由配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用laravel,目前使用的是5.2版. 我正在使用两种形式来获取数据.第一视图中的第一种形式,第二视图中的第二种形式.我需要找出一种方法来告诉第二种形式,即第二种形式将链接到的第一种形式的形式ID.

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.

我知道这可以通过使用URL传递值来完成.但是我缺乏正确语法的知识.

I know this can be done by passing the values using the URL. But I lack the knowledge of the correct syntax.

1-如何在重定向时发送数据?

1- How to send data while redirecting?

2-路线应如何显示?

2- How should the route look like?

3-如何在第二个视图中访问该值,以便在第二个表单提交时传递该值?

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.

谢谢.

这是控制器中的代码:

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

这些表单是普通的html表单,具有提交方式 当我使用dd(Session::get('value'));时,我得到空值.这表示未传递值.提交第二个表单时调用的postCreateRequirement方法.

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.

下面是路线.

//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-如何在重定向时发送数据?

您可以使用->with()方法通过重定向直接传递数据,该方法创建一个会话,该会话将仅显示在下一页上.

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-路线的外观如何?

应该在http目录的routes.php文件中定义路由,假设您要发布数据,则应制作路由并将其连接到控制器,如下所示:

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-如何在第二个视图中访问该值,以便在提交第二个表单时传递该值?

您只需在重定向中使用->with()方法

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

获取价值

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 创建一个全局会话,然后刷新它

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

获取价值

public function getCreateRequirement(Request $request){
    return view('req');
}

public function postCreateRequirement(Request $request){
    $value = session("value");
    $request->session()->forget("value");
}

注意:当有人注销时,Laravel不会刷新会话,如果不手动或使用方法$request->session()->flush();

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

这篇关于将值从一个视图传递到其他laravel路由配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆