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

BKF
  • 1,298
  • 4
  • 16
  • 35

2 Answers2

11

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Saiyan Prince
  • 3,930
  • 4
  • 28
  • 71
  • Great !!! Thanks a lot for you help ! but there's still a problem : how could I validate fields before sending them to update method in controller ? because here if I press submit button without filling required fields (should get errors and stay on modal with old values) the Modal hides and the $user doesn't update :/ – BKF Dec 26 '15 at 18:15
  • Any tutorial about validating modal by Ajax ? thanks :) – BKF Dec 26 '15 at 18:18
  • Not sure but laracasts should help you out with what you are looking for.. Or simply google: laravel ajax validation – Saiyan Prince Dec 26 '15 at 18:21
  • May be this can give you an idea... Check it out... https://laracasts.com/discuss/channels/general-discussion/showing-request-validation-errors-when-submitting-form-by-ajax – Saiyan Prince Dec 26 '15 at 18:26
2

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

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

siralexsir88
  • 418
  • 1
  • 4
  • 14
Amir Bar
  • 3,007
  • 2
  • 29
  • 47
  • Thanks for answer, but I can't resend $user from view to modal by this code ! this is work for defined value for example : @include(('user.edit', ['user' => 'value']) But I want to resend the hole user passed in view via controller, and in modal I can retrieve user's data like this : {!! $user->username !!} – BKF Dec 26 '15 at 17:28
  • you are wrong, the code I gave you is working, the user.edit view will have the all $user data – Amir Bar Dec 26 '15 at 17:57
  • sorry but how could you set your code to send $user ? once you pass $user to view, you can just retrieve its fields, so if you tried it please set a code to follow you :) – BKF Dec 26 '15 at 18:05
  • I think there is misunderstanding from me,what you really asking is: how to pass data from js to php? – Amir Bar Dec 26 '15 at 18:07