3

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 :)

4

2 回答 2

10

试试这个方法。我正在使用a标签,但该解决方案也应该适用于您的按钮。

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

jQuery代码:

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

id使用传入的方法在模式窗口内创建输入字段find

这会将输入字段中传递的值放在模式窗口内。

于 2015-12-26T17:58:51.930 回答
2

您可以使用刀片include()函数,该函数接受一个数组作为第二个参数:

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

于 2015-12-26T17:17:11.830 回答