How to create session and set life time follow/unfollow in Laravel -


i trying function follow/unfollow in laravel. code working.

model:

public function followers() {     return $this->belongstomany('app\user', 'followers', 'user_id', 'follower_id'); }  public function follow(user $user) {     $this->followers()->attach($user->id); }  public function unfollow(user $user) {     $this->followers()->detach($user->id); } 

controller:

public function edit($id) {     $user = user::find($id);     auth::user()->follow($user); }  public function delete($id) {     $user = user::find($id);     auth::user()->unfollow($user); } 

html:

<button id="follow" class="btn"><i class="fa fa-user-plus"></i> follow</button> 

ajax:

$(document).on('click', '#follow', function (e) {     var followid = $('#follow');     $.ajax({         url: '{{ route("profile.edit", $user->id) }}',         type: 'get',         success: function () {             followid.html('<i class="fa fa-user-times"></i>' + ' unfollow');             $('#follow').attr('id', 'unfollow');         }     }); });  $(document).on('click', "#unfollow", function (e) {     var unfollowid = $('#unfollow');     $.ajax({         url: '{{ route("profile.delete", $user->id) }}',         type: 'get',         success: function () {             unfollowid.html('<i class="fa fa-user-plus"></i>' + ' follow');             $('#unfollow').attr('id', 'follow');         }     }); }); 

now, want when reloading page or exit browser, code keeps in mind. session, , how set lifetime forever? vote helpful answers. all.

i assume using table named followers purpose. have these scenarios:

  1. there no record currentuser vs targetuser, means no follow nor unfollow
  2. if there record exists, there must someflag(followstatus) in table set true, decide follow case
  3. if followstatus = false, unfollow case.

now on page load, within action method using display view, check above 3 cases, , put variable in session. e.g. 0 means no record exists, 1 means followed, 2 means unfollowed.

session(['targetuserstatus' => '0']); // no record session(['targetuserstatus' => '1']); // followed session(['targetuserstatus' => '2']); // unfollowed 

now within blade template check session variable:

@if(session::get('targetuserstatus') == "0")   //set ui - no record case @else@if(session::get('targetuserstatus') == "1")  //set ui - followed @else  //set ui - unfollowed @endif 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -