Laravel:如何在刀片中将变量从一页发送到另一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39880340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:34:11  来源:igfitidea点击:

Laravel : How to send variable from one page to another in blade

phplaravellaravel-5laravel-blade

提问by Hola

I'm building a laravel application where from The first page I want to send a variable or say value to second page in a form ..How do I do it? Let's say In my first page I have several buttons so when a user click on any one button, he will be redirected to another page where he has to submit a form. In first page while he select a button, he automatically select a course_id which will also submit inside the second page form. But How do i send the course_id from the first page button?

我正在构建一个 Laravel 应用程序,从第一页我想发送一个变量或说值到表单中的第二页..我该怎么做?假设在我的第一页中,我有几个按钮,因此当用户单击任何一个按钮时,他将被重定向到另一个必须提交表单的页面。在第一页中,当他选择一个按钮时,他会自动选择一个 course_id,该 ID 也将在第二页表单中提交。但是如何从第一页按钮发送 course_id?

<li> <a  href="{{route('registration')}}">A</a> </li>
<li> <a href="{{route('registration')}}" >B</a> </li> 

When a user click on the button second page will appear ..Where I'm gonna submit a form where course_id will come from the first page means the <a>tag

当用户单击按钮时,第二页将出现..我要提交一个表单,其中 course_id 将来自第一页意味着<a>标签

Here is my form demo:

这是我的表单演示:

{!! Form::open(array('route' => 'postRegistration','class'=>'form-horizontal','method'=>'POST'))  !!}
{!! Form::token(); !!}
{!! csrf_field() ; !!} 

<div class="form-group">
    <label class="sr-only" for="form-first-name">Name</label>
    <input type="text" name="name" placeholder="Name..." class="form-first-name form-control" id="form-first-name">
</div>
<div class="form-group">
    <label class="sr-only" for="form-email">Email</label>
    <input type="text" name="email" placeholder="Email..." class="form-email form-control" id="form-email">
</div>

<div class="form-group">
    <label class="sr-only" for="form-last-name">Address</label>
    <textarea rows="4" style="height:100px;" name="address" placeholder="Address..." class="form-last-name form-control" id="form-last-name"></textarea>
</div>
<button type="submit" class="btn">Submit</button>

{!! Form::close() !!}

In my database I have to submit the above field as well as the course_id from the database. How do I do it in Laravel?

在我的数据库中,我必须提交上述字段以及数据库中的 course_id。我如何在 Laravel 中做到这一点?

Any suggestion or solution please?

请问有什么建议或解决方案吗?

回答by Risul Islam

You can send the variable as route parameter. To do this change your <a>tags like this.

您可以将变量作为路由参数发送。为此,请<a>像这样更改您的标签。

<li> <a href="{{route('registration', ['course_id' => 'A'])}}">A</a> </li>
<li> <a href="{{route('registration', ['course_id' => 'B'])}}">B</a> </li> 

Then you need to allow your registrationroute to take parameter. Suppose your route is like this

然后你需要让你的registration路线接受参数。假设你的路线是这样的

Route::get('/registration', [
    'as' => 'registration', 'uses' => 'SomeController@method'
]);

Change it to

将其更改为

Route::get('/registration/{course_id}', [
    'as' => 'registration', 'uses' => 'SomeController@method'
]);

Now you have to add this parameter into your SomeController@method

现在您必须将此参数添加到您的 SomeController@method

public method($course_id){ ... }

Now pass this $course_idto your form view. To submit this variable with your other fields you can add this as hidden input field.

现在将其传递$course_id给您的表单视图。要将此变量与其他字段一起提交,您可以将其添加为隐藏输入字段。

<div class="form-group">
    <input type="hidden" name="course_id" value="{{ $course_id }}">
</div>

回答by Shrinivas Ambat

So you want to create page with multiple course links and each link redirects to registration page with the course id to be used in the registration form.

因此,您希望创建具有多个课程链接的页面,并且每个链接都重定向到带有要在注册表中使用的课程 ID 的注册页面。

Your a href link should look like this

您的 href 链接应如下所示

{!! link_to_route('registration', 
$title = 'Course 1', $parameters = ['1'], $attributes = 
['class'=>'btn btn-success']) !!}

Routes file web.php

路由文件web.php

Route::get('/registration/{course_id}','
CourseRegistration@showForm')->name('registration');

CourseController CourseController

课程控制器课程控制器

public function showForm($course_id){
    return view('registration')->with('courseid',$course_id);
}

Now you can access the course id with $courseid in view. If you want to pass it in form create a hidden or input tag with the data.

现在您可以在视图中访问带有 $courseid 的课程 ID。如果您想在表单中传递它,请创建一个带有数据的隐藏或输入标签。

回答by Saumya Rastogi

According to me, you're looking for a type of web app in which when a user submits a form the page opens up with the registered user details. This can be done in Laravel like this:

根据我的说法,您正在寻找一种网络应用程序,当用户提交表单时,页面会打开并显示注册用户的详细信息。这可以在 Laravel 中完成,如下所示:

routes.php

路由文件

// For submitting form on this route
Route::post('users/register', 'UsersController@create')->name('users.register');
// For showing user's profile via user's id
Route::get('users/{id}/profile', 'UsersController@show')->name('users.profile');

UsersController.php

用户控制器.php

class UsersController {

    public function create() {
        $inputs = request()->all();
        // Use Eloquent to save user info to DB and fetch insert id from DB...
        return redirect()->route('users.profile', array('id' => $insert_id));
        // Will open registered user profile page
    }

    public function show($id) {
        $user = User::find($id);
        if(!$user) {
            abort('404');
        }
        return view('users/profile', compact('user'));
        // At the users/profile.php page you can use $user variable to populate user's info
    }

}

Usage of route method with passing data with route in Blade

使用路由方法在 Blade 中通过路由传递数据

{{ route('users.profile', array('id' => $user->id)) }}

For more help, see this >>

如需更多帮助,请参阅此>>

Hope this helps you!

希望这对你有帮助!