//创建控制器 php artisan make:controller DemoController //创建模型层 php artisan make:model Models/login //登录 php artisan make:model Models/list //列表 php artisan make:model Models/cang //收藏
路由:
//登录 收藏 Route::group([‘prefix‘=>‘demo‘],function (){ //登录页面 Route::get(‘login‘,function (){ return view(‘index.login‘); })->name(‘login‘); //登录处理 Route::post(‘dologin‘,‘DemoController@dologin‘)->name(‘dologin‘); //列表页 Route::get(‘list‘,‘DemoController@list‘)->name(‘list‘); //收藏 Route::get(‘cang‘,‘DemoController@cang‘)->name(‘cang‘); //取消收藏 Route::get(‘qu‘,‘DemoController@qu‘)->name(‘qu‘); //退出登录 Route::get(‘logout‘,‘DemoController@logout‘)->name(‘logout‘); });
控制器
<?php namespace App\Http\Controllers; use App\Models\cang; use App\Models\listt; use App\Models\login; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cookie; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Session; class DemoController extends Controller { //登录处理 public function dologin(Request $request){ $this->validate($request,[ ‘name‘ => ‘required‘, ‘password‘ => ‘required‘ ],[ ‘name.required‘ => ‘用户名必填‘, ‘password.required‘ => ‘密码必填‘ ]); $data = $request->except(‘_token‘); //加密的密码 $data[‘password‘] = md5($data[‘password‘]); //调用模型层 $res = login::Login($data); if($res){ Cookie::queue(‘name‘,$data[‘name‘]); // Session::put(‘key‘,‘value‘); return redirect(route(‘list‘)); } return redirect(route(‘login‘)); } //列表 public function list(){ //获取数据 $data = listt::paginate(4); return view(‘index.list‘,[‘data‘=>$data]); } //收藏 public function cang(Request $request){ $de[‘list_id‘] = $request[‘id‘]; //收藏的id $de[‘login_name‘] = Cookie::get(‘name‘); //用户名 $re = DB::table(‘cang‘)->where(‘list_id‘,‘=‘,$de[‘list_id‘])->where(‘login_name‘,‘=‘,$de[‘login_name‘])->get()->toArray(); if (empty($re)){ cang::create($de); //收藏的数据表 $aa = DB::table("list")->where(‘id‘,$de[‘list_id‘])->increment(‘cang‘,1);//自增1 return [‘code‘=>‘0‘,‘msg‘=>‘收藏成功‘]; } } //取消收藏 public function qu(Request $request){ $de[‘list_id‘] = $request[‘id‘]; //收藏的id $de[‘login_name‘] = Cookie::get(‘name‘); //用户名 $re = DB::table(‘cang‘)->where(‘list_id‘,‘=‘,$de[‘list_id‘])->where(‘login_name‘,‘=‘,$de[‘login_name‘])->delete(); return [‘code‘=>‘0‘,‘msg‘=>‘取消收藏成功‘]; } //退出登录 public function logout(Request $request){ $cookie = Cookie::forget(‘name‘); return redirect(route(‘login‘))->withCookie($cookie)->withErrors(‘退出成功‘); } }
模型层:
收藏:cang
//绑定数据表 protected $table = ‘cang‘; protected $guarded = [];
列表:list
//绑定数据表 protected $table = ‘list‘; //开启软删除 use SoftDeletes; protected $dates = [‘deleted_at‘];
登陆:login
//绑定数据表 protected $table = ‘login‘; //登录 public static function Login($data){ return self::where($data)->first(); }
视图层:index文件夹 list、login、validate
登录页:
<html> <div class="big"> <div class="form"> <form action="{{route(‘dologin‘)}}" method="post"> {{csrf_field()}} <table>    <tr> <td>账号</td> <td><input type="text" name="name" placeholder="请输入账号" class="a"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password" placeholder="请输入密码" class="a"></td> </tr> <td></td> <td>  <input type="submit" value="提交"></td> <td><input type="reset" value="重置"></td> </table> @include(‘index.validate‘) </form> </div> </div> </html> <style> .big{ height: 1000px; width: 900px; background-color: beige; float: left; } .form{ height: 400px; background-color: azure; margin-top: 200px; } table{ margin-left: 200px; } tr{ line-height: 100px; } td{ display: inline-block; margin-left: 50px; } .a{ border-radius: 5px; height: 30px; width: 250px; } *{ font-family: "华文楷体"; font-size: 21px; }
列表页:
<!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <link src="/lib/layer/2.4/layer.js"></link> <script src="/lib/jquery/1.9.1/jquery.js"></script> {{--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>--}} <html> <script> </script> <body> <div class="header"> <a href="{{route(‘logout‘)}}" class="b">退出</a> </div> <table class="table table-hover"> <tr> <td>ID</td> <td>标题</td> <td>简介</td> <td>图片</td> <td>收藏人数</td> <td>创建时间</td> <td> 操作 </td> </tr> @include(‘index.validate‘) @include(‘index.success‘) @foreach($data as $v) <tr> <td>{{$v[‘id‘]}}</td> <td>{{$v[‘title‘]}}</td> <td>{{$v[‘msg‘]}}</td> <td><img src="{{$v[‘img‘]}}" style="height: 150px;width: 130px"></td> <td> <a href="" class="label label-success radius">{{$v[‘cang‘]}}</a></td> <td>{{$v[‘created_at‘]}}</td> <td> <a href="" class="label label-primary radius">修改</a> <a href="" class="label label-danger radius">删除</a> @if($v[‘deleted_at‘] !=null) <a href="#" class="label label-default radius cang" onclick="sc({{$v[‘id‘]}})" id="{{$v[‘id‘]}}">收藏</a> @else <a href="#" class="label label-default radius cang" onclick="sc({{$v[‘id‘]}})" id="{{$v[‘id‘]}}">收藏</a> @endif </td> </tr> @endforeach </table> {{$data->links()}} </body> </html> <style> body{ background-repeat: no-repeat; background-size: 100%; opacity:0.8; } .a{ display: inline; font-size: 20px; font-family: "华文楷体"; margin-left: 10px; text-decoration: none; } .b{ display: inline; font-size: 20px; font-family: "华文楷体"; text-decoration: none; float: right; margin-right: 20px; } a:hover{ color: red; } .header{ height:40px; width: 100%; background-color: #dedede; line-height: 40px; border-radius: 4px; } table{ text-align: center; font-weight: bold; font-family: "华文楷体"; font-size: 22px; text-align: center; } a{ text-decoration: none; } tr{ margin-top: 10px; } </style> <script> function sc(ee) { let id = ee; // alert(id) console.log(name) if ( $(‘#‘+id).text() == ‘收藏‘){ $.ajax({ url:"http://w.demo.com/demo/cang", data:{‘id‘:id}, type:‘GET‘, dataType:‘json‘, success:function (data) { $(‘#‘+id).html("取消收藏") alert(data.msg) // window.location.reload() //刷新页面 } }) }else if( $(‘#‘+id).text() == ‘取消收藏‘){ $.ajax({ url:"http://w.demo.com/demo/qu", data:{‘id‘:id}, type:‘GET‘, dataType:‘json‘, success:function (data) { $(‘#‘+id).html("收藏") alert(data.msg) } }) } } </script>
错误消息页:
@if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif
原文:https://www.cnblogs.com/fsp69/p/14078801.html