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

2 Answers 11

blade include() function getting array as second parameter:

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

upvote
  flag
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
upvote
  flag
you are wrong, the code I gave you is working, the user.edit view will have the all $user data – Amir Bar
upvote
  flag
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
upvote
  flag
I think there is misunderstanding from me,what you really asking is: how to pass data from js to php? – Amir Bar
up vote 3 down vote accepted

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

Hope this helps you out. Happy coding.. Cheers.

upvote
  flag
Ok, I'll try it :) – BKF
upvote
  flag
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
upvote
  flag
You need to use ajax for that.. – Saiyan Prince
upvote
  flag
Any tutorial about validating modal by Ajax ? thanks :) – BKF
upvote
  flag
Not sure but laracasts should help you out with what you are looking for.. Or simply google: laravel ajax validation – Saiyan Prince
upvote
  flag
May be this can give you an idea... Check it out... laracasts.com/discuss/channels/general-discussion/… – Saiyan Prince
upvote
  flag
Thank you very much man :)) – BKF
upvote
  flag
Most welcome... – Saiyan Prince

Not the answer you're looking for? Browse other questions tagged or ask your own question.