简体   繁体   中英

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

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?

<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

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?

Any suggestion or solution please?

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

<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 registration route to take parameter. Suppose your route is like this

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

public method($course_id){ ... }

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

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

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.

Your a href link should look like this

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

Routes file 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.

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:

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

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

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

For more help, see this >>

Hope this helps you!

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