1

I'm actually running into little problems with my current project. Following case:

I've got a model called "Posting" with relations:

public function subscribers(){
    return $this->belongsToMany('User');    
}

In my view-file there is a table containing all Postings and also a checkbox for subscribing/unsubscribing with the matching value to the posting-id:

<input class="click" type="checkbox" name="mobileos" value="{{{$posting->id}}}"
     @if($posting->subscribers->find(Auth::User()->id)) 
         checked="checked"
     @endif
>

Now the thing I want to archive:

A JavaScript is going to watch if the checkbox is checked or not. According to that, the current user subscribes/unsubscribes to the posting. Something like:

        $('.click').on('click',function() {

            // $posting->find(---$(this).prop('checked')---)->subscribers()->attach(---Auth::user()->id---);
            // $posting->find(---$(this).prop('checked')---)->subscribers()->detach(---Auth::user()->id---);
        });

Is there any possibility to archieve that or any other ways? I couldn't get my head around this so far.

Cheers, Chris

2
  • You will need to communicate with Laravel through Ajax calls. Did you try that already or are you familiar with Ajax? Commented Mar 11, 2015 at 20:25
  • Not really familiar, just basic skills. But if that's the right way digging into that shouldn't be a problem :) Commented Mar 11, 2015 at 20:30

1 Answer 1

1

If you want to use Ajax to achieve this, you will need a REST endpoint in Laravel for the subscriptions, e.g.:

http://localhost/subscribe/{{userid}}

When this Endpoint is called, the database can be updated. The function could also return a JSON showing, if the saving database in the database successful.

Use this endpoint to make an Ajax Call on click:

var user = {
   id: 0 // retrieve the correct ID from wherever it is stored
}

$('.click').on('click',function() {

  $.GET('http://localhost/subscribe/' + user.id, 
  function () { // this is the success callback, that is called, if the Ajax GET did not return any errors
    alert('You are subsribed')
  });

});

Ideally you won't be using the GET method, but instead use POST and send the user ID as data. Also you would need to retrieve the user ID from session or wherever it is stored.

Take care that as you are using Ajax it can easily be manipulated from the client side. So on the server you should check, if the user ID that was sent is the same as in the Session. Maybe you don't need to send the user id at all, but that depends on how your backend is built.

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

2 Comments

Works like charm, thank you! I created additional POST-Routes for subscribing and unsubscribing and posted the posting-ID to it via AJAX :)
You're Welcome. Don't forget to read on how to secure a REST interface ;)

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.