laravel框架之關注/取消關注
阿新 • • 發佈:2018-12-19
以下為控制器部分程式碼
/** *顯示我的粉絲及我的關注 *@param $fan我的粉絲 *@param $follow我的關注(兩表聯查) */ public function fans(){ $fans = Login::fans(); $follow = Login::followshow(); $count = count($follow); return view('Fans/fans_show',['fans'=>$fans,'follow'=>$follow,'count'=>$count]); } //關注 public function insert(){ $info = Input::get(); $res = Login::insertFollow($info); echo $res; } //取消關注 public function delfans(){ $info = Input::get('id'); $res = Login::fandel($info); echo $res; }
以下為模型中部分程式碼
//顯示我的粉絲 public static function fans(){ $res = DB::table('jy_fans')->get(); return $res; } //我的關注 public static function followshow(){ $uid = Session::get('id'); $sql = "select * from jy_follow inner join jy_fans on jy_follow.fid = jy_fans.fid where jy_follow.uid = $uid"; $result = DB::select($sql); return $result; } //點選關注實現入庫 public static function insertFollow($arr){ //print_r($arr);die; $uid = Session::get('id'); $array = array( 'uid' => $uid, 'fid' => $arr['id'] ); $res = DB::table('jy_follow')->insert($array); return $res; } //取消關注 public static function fandel($id){ $res = DB::table('jy_follow')->where('id',$id)->delete(); return $res; }
下面為模板程式碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>我的好友</title> <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/> <meta content="yes" name="apple-mobile-web-app-capable"/> <meta content="black" name="apple-mobile-web-app-status-bar-style"/> <meta content="telephone=no" name="format-detection"/> <link href="{{asset('Follow')}}/css/style.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="{{asset('Follow')}}/js/jquery.min.js"></script> <script type="text/javascript" src="{{asset('Follow')}}/js/tab.js"></script> </head> <body> <section class="aui-flexView"> <header class="aui-navBar aui-navBar-fixed"> <a href="javascript:history.back()" class="aui-navBar-item"> <i class="icon icon-return"></i> </a> <div class="aui-center"> <span class="aui-center-title">我的好友</span> </div> <a href="javascript:;" class="aui-navBar-item"> <i class="icon icon-sys"></i> </a> </header> <section class="aui-scrollView"> <div class="aui-tab-box" data-ydui-tab> <ul class="tab-nav"> <li class="tab-nav-item tab-active"> <a href="javascript:;">我的關注</a> </li> <li class="tab-nav-item"> <a href="javascript:;">我的粉絲</a> </li> </ul> <div class="tab-panel"> <div class="tab-panel-item tab-active"> <div class="tab-item"> @foreach($follow as $k => $v) <a href="javascript:;" class="aui-list-item"> <div class="aui-list-item-hd"> <img src="{{$v->image}}" alt=""> <span class="aui-well-comm-vip"> <img src="{{asset('Follow')}}/images/icon-vip.png" alt=""> </span> </div> <div class="aui-list-item-bd"> <h4>{{$v->username}}</h4> <p>{{$v->content}}</p> </div> <span class="aui-list-item-fr" onclick="qx({{$v->id}})"> <img src="{{asset('Follow')}}/images/icon-follow-001.jpg" alt=""> <p>@if(!empty($follow))取消關注@endif</p> </span> </a> @endforeach </div> </div> <div class="tab-panel-item"> <div class="tab-item"> @foreach($fans as $k => $v) <a href="javascript:;" class="aui-list-item"> <div class="aui-list-item-hd"> <img src="{{$v->image}}" alt=""> <span class="aui-well-comm-vip"> <img src="{{asset('Follow')}}/images/icon-vip.png" alt=""> </span> </div> <div class="aui-list-item-bd"> <h4>{{$v->username}}</h4> <p>{{$v->content}}</p> </div> <span class="aui-list-item-fr" onclick="getFollow({{$v->fid}})"> <img src="{{asset('Follow')}}/images/icon-follow-003.jpg" alt=""> @for($i=0;$i<$count;$i++) <p style="color:#ce0000"> @if($v->fid == $follow[$i]->fid)已關注@endif </p> @endfor </span> </a> @endforeach </div> </div> </div> </div> </section> </section> </body> </html> <!-- <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> --> <script type="text/javascript"> function getFollow(a){ $.ajax({ type:"GET", url:"{{url('addfollow')}}", data:{id:a}, success:function(e){ if(e==1){ window.location.href=""; } } }) } function qx(a){ // alert(a) $.ajax({ type:"GET", url:"{{url('fansdel')}}", data:{id:a}, success:function(e){ if(e==1){ window.location.href=""; } } }) } </script>