WellNo
WellNo

Reputation: 659

Can't send data with AJAX to Laravel Controller

I'm trying to do detach some data from my database. I'm trying to send 2 different data to my controller with Ajax but couldn't make it. I need the domain_id and the tag_id.

Thats how it looks right now:

@foreach($domains as $domain)
    <tr>
        <td>
            @foreach($domain->tags as $tag)
                <span><a href="#" data-tag-id="{{ $tag->id }}"
                         data-domain-id="{{ $domain->id }}">{{ $tag->name }}</a></span>
            @endforeach
        </td>
    </tr>
@endforeach

Now I have the domain_id and the tag_id in the <a> tag. I'm trying to send both of them to my controller with Ajax, To detach them from my pivot table and thats it.

JS code:

$('a[data-tag-id]').click(function () {
    var tag_id = $(this).attr('data-tag-id');
    var domain_id = $(this).attr('data-domain-id');
    $.ajax({
        url: 'detaching',
        type: 'GET',
        data: {tag_id: tag_id, domain_id: domain_id},
        dataType: 'json',
        success: function (data) {
            console.log('worked!');
        }
    });
});

Route Code:

Route::get('detaching', 'DomainController@detach2');

Controller Code:

public function detach2()
{
    $input =  Input::get('all');
    $domain = Domains::findOrFail($input['domain_id']);
    $domain->tags()->detach($input['tag_id']);
}

This don't work and I don't even know if the gets Ajax code called. I'm trying to console.log() some things out but I don't get anything back. I don't even know if the code reaches the controller function. I don't have much knowledge about Ajax or JS at all. Can someone maybe help me there?


LITTLE UPDATE

I've tried an easy:

$('a[data-tag-id]').click(function () {
    console.log('YES');
});

But this haven't worked either, So it doesn't jumps in the click function all I guess.

Upvotes: 0

Views: 553

Answers (1)

Raunak Gupta
Raunak Gupta

Reputation: 10799

Try This,
Give a class to your anchor tag like.

<a href="#" class="get-link" data-tag-id="{{ $tag->id }}" data-domain-id="{{ $domain->id }}">{{ $tag->name }}</a>

Change your route to

Route::get('detaching/{tag_id}/{domain_id}', 'DomainController@detach2');

Chnage your controller function to

public function detach2($tag_id, $domain_id)
{
    //$input =  Input::get('all');
    $domain = Domains::findOrFail($domain_id);
    $domain->tags()->detach($tag_id);
}

Change your JS file to

(function ($) {
    $(document).ready(function () {
        $(document).off('click', '.get-link');
        $(document).on('click', '.get-link', function () {
            var domain_id = $(this).data('domain-id');
            var tag_id = $(this).data('tag-id');
            $.ajax({
                type: 'GET',
                url: 'detaching'+ '/' + tag_id + '/' + domain_id + '/',
                success: function (data) {
                    console.log('worked!');
                },
                error: function (data) {
                    console.log('Error:', data);
                }
            });
        });
    });
})(jQuery);

Upvotes: 1

Related Questions