1

I am using jquery to get data from controller from controller i am returning a $user from controller method but don't know how to use it in view.

Here is my Code..

    <li>
<a href="#suppression" data-toggle="modal" onclick="getForm('{{ $data->id }}')"> Get Modal</a>
</li>

and my jquery method is

function getForm(id)
{
    $.ajax({
        url: '/student-forms/get-form/'+id,
        type: "GET",
    });
}

Controller method is

public function getForm($id)
{

    $user = DB::table('users')->select('name','type','options')->whereIn('id' , $id)->get();
    return $user;

}

I want to get above variable $user here in my model

<div id="suppression" class="modal" role="dialog" aria-hidden="true">
<div class="modal-dialog" style="width: 500px;">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        </div>
        <div class="modal-body">

        </div>
    </div>
</div>

Please help how can i achieve using above jquery method Thanks

4
  • 1
    You will have to use jquery to append that information to the dom in the success method of your ajax request. Commented May 10, 2017 at 11:23
  • Do you want to print $user in modal-body? Commented May 10, 2017 at 11:23
  • @Joe please help how to append Commented May 10, 2017 at 11:24
  • @bluemoon see my answer. Commented May 10, 2017 at 11:26

3 Answers 3

1

Change your ajax to append the data to the page when the ajax is executed successfully

function getForm(id)
{
    $.ajax({
        url: '/student-forms/get-form/'+id,
        type: "GET",
        success:function(data) {
          $('modal-body').append(data.name);
        }
    });
}

Remove the onclick event , add a attribute to the link to store the id

<a href="#suppression" data-toggle="modal" data-id="{{ $data->id }}"> Get Modal</a>

trigger the ajax when the modal is shown

$('#suppression').on('show.bs.modal', function (e) {
  var id = $(e.relatedTarget).attr('data-id');
  getForm(id);
})

Controller should return json

public function getForm($id)
{

    $user = DB::table('users')->select('name','type','options')->whereIn('id' , $id)->get();
    return response()->json($user);

}
Sign up to request clarification or add additional context in comments.

10 Comments

Laravel DB Facade already returns Collection. You don't need to encode it.
is your modal opening?
then the show.bs.modal isn't triggered, are you sure the modal opens the default bootstrap way?
try the shown.bs.modal event and drop a console.log in there to see if the event triggers
what is data.name in jquery method above in your answer @madalinivascu
|
0

As you're using jQuery you'll need to also use it to edit the dom.

$.ajax({
    url: '/student-forms/get-form/'+id,
    type: "GET",
    success: function(response){
        $('#elementToChange').text(response.thingfromyourusermodel)
    }
});

2 Comments

dear please explain what will be thingfromyourusermodel
@bluemoon response will be the response from your controller ajax call, so it going on your code it will contain the name, type and options attributes from your user model.
0

If you use jQuery you can use this code:

function getForm(id)
{
    $.ajax({
        url: '/student-forms/get-form/'+id,
        type: "GET",
    }).done(function(msg) {
        $(".modal-body").val(msg);
    });
}

It will add returned value in your modal-body div. Since Laravel returns collection you will need format your user data.

e.g

.val('name:' + msg.name);

By the way don't forget to add crsf token field to your forms.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.