Laravel 5.1 pass data from view to modal

25,545

Solution 1

Try out this way. I am using a tag, but the solution should work for you as well with button.

<a
    href="#"
    data-target="yourModalId"
    data-toggle="modal"
    data-email="{{ $user->email }}"
    data-username="{{ $user->username }}"
 >
     Edit
</a>

jQuery code:

$('#yourModalId').on('show', function(e) {
    var link     = e.relatedTarget(),
        modal    = $(this),
        username = link.data("username"),
        email    = link.data("email");

    modal.find("#email").val(email);
    modal.find("#username").val(username);
});

Create the input fields inside the modal window with the id that are passed in find method.

That will put the values passed in the input fields inside modal window..

Solution 2

You can use the blade include() function, which accepts an array as the second parameter:

@include('user.edit', ['user' => $user])

Share:
25,545
BKF
Author by

BKF

Engineer in computer science (Information System)

Updated on July 09, 2022

Comments

  • BKF
    BKF almost 2 years

    How can I pass data from blade view to modal dialog : For example : I pass $user from controller to view :

     $user = User::findOrFail($id);
     return view('user.show')->withUser($user);
    

    Next, I want to pass this data $user to a modal included in this view via a button like this :

    @include('user.edit',$user);

    and there in the modal I can set $user's values (like this : {!! $user->lastname !!} ) to edit them for example.

    Please Help me :)