IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> laravel CURD 教学 详解 -> 正文阅读

[PHP知识库]laravel CURD 教学 详解

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\http\Middleware;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

//Route::get('/', function () {
//    return view('welcome');
//});
Route::prefix('loge')->group(function (){
    Route::any('loge','logeController@loge')->name('loge');
    Route::any('logging','logeController@logging')->name('logging');
});
Route::prefix('admin')->group(function (){
    Route::any('form','UserController@form')->name('form')->middleware('CheckLog');
    Route::any('add','UserController@add')->name('add');
    Route::any('show','UserController@show')->name('show');
    Route::any('del','UserController@del')->name('del');
    Route::any('upShow','UserController@upShow')->name('upShow');
    Route::any('upShow','UserController@upShow')->name('upShow');
    Route::any('upData','UserController@upData')->name('upData');
    Route::any('huishouzhan','UserController@huishouzhan')->name('huishouzhan');
    Route::any('cd','UserController@cd')->name('cd');
    Route::any('hf','UserController@hf')->name('hf');
});
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreBlogPost;
use App\models\loge;
use Illuminate\Http\Request;
use Illuminate\http\Middleware;
use Illuminate\Support\Facades\Auth;
use MongoDB\Driver\Session;

class logeController extends Controller
{

    public function loge()
    {
        return view('loge');
    }
    public function logging(StoreBlogPost $request)
    {
        $data = $request->validated();
        $uname = $request->input('uname');
        $pwd = $request->input('pwd');

        $obj = new loge();
        $res = $obj->loge($uname);
        $arr = json_decode(json_encode($res),true);

        if ($arr['uname']==$uname)
        {
            if ($arr['pwd']==$pwd)
            {
                return view('form');
            }else
            {
                return view('loge')->withErrors('密码错误');
            }
        }else{
            return view('loge')->withErrors('账号错误');
        }

    }
}
<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;

class loge extends Model
{
    protected $table = 'loge';
    public $timestamps = false;
    public function loge($uname)
    {
        return $this->where('uname',$uname)->first();
    }
}
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <form action="{{ route('logging') }}" method="post" enctype="multipart/form-data">
        @csrf
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        <div class="form-group">
            <label for="email">账号</label>
            <input type="text" class="form-control" name="uname">
        </div>
        <div class="form-group">
            <label for="pwd">密码</label>
            <input type="password" class="form-control" name="pwd">
        </div>
        <div class="row">
            <div class="col-md-8">
                <input type="text" class="form-control {{$errors->has('captcha')?'parsley-error':''}}" name="captcha" placeholder="captcha">
            </div>
            <div class="col-md-4">
                <img src="{{captcha_src()}}" style="cursor: pointer" onclick="this.src='{{captcha_src()}}'+Math.random()">
            </div>
            @if($errors->has('captcha'))
                <div class="col-md-12">
                    <p class="text-danger text-left"><strong>{{$errors->first('captcha')}}</strong></p>
                </div>
            @endif
        </div>
        <button type="submit" class="btn btn-primary">登录</button>
    </form>
</div>

</body>
</html>
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreBlogPosts;
use App\models\user;
use Illuminate\Http\Request;

class UserController extends Controller
{

    public function __construct()
    {
        $this->middleware('CheckLog',[
            'only'=>['edit','update']
        ]);
    }
    public function form()
    {
        return redirect('admin/form');
    }
    public function add(StoreBlogPosts $request)
    {
        $data = $request->validated();
        $data['img'] = $request->file('img')->store('img');
        $obj = new user();
        $res = $obj->add($data);
        if ($res)
        {
            echo "<script>alert('添加成功');location.href='".route('show')."'</script>";
        }
        else
        {
            echo "<script>alert('添加失败');location.href='".route('form')."'</script>";
        }
    }
    public function show(Request $request)
    {
        $uname = $request->input('uname');
        $arr1 = [];
        $arr2 = [];
        if (!empty($uname))
        {
            array_push($arr1,['uname','like','%$uname%']);
            $arr2['uname'] = $uname;
        }
        $obj = new user();
        $res = $obj->show($arr2);
        return view('show',compact('res'),['arr2'=>$arr2]);
    }
    public function del(Request $request)
    {
        $id = $request->input('id');
        $obj = new user();
        $res = $obj->del($id);
        if ($res)
        {
            echo json_encode(['code'=>'200','msg'=>'成功','data'=>$res]);
        }

    }
    public function upShow()
    {
        $id = request()->get('id');
        $obj = new user();
        $res = $obj->upShow($id);
        return view('upShow',compact('res'));
    }
    public function upData()
    {
        $data = request()->post();
        unset($data['_token']);
        $id = request()->get('id');
        $obj = new user();
        $res = $obj->upData($id,$data);
        if ($res)
        {
            echo "<script>alert('修改成功');location.href='".route('show')."'</script>";
        }
        else
        {
            echo "<script>alert('修改失败');location.href='".route('show')."'</script>";
        }
    }
    public static function huishouzhan()
    {
        $res = user::huishouzhan();
        return view('huishouzhan',['res'=>$res]);
    }
    public static function cd(Request $request)
    {
        $id = $request->get('id');
      user::cd($id);
        return redirect('admin/show');
    }
    public static function hf(Request $request)
    {
        $id = $request->get('id');
       user::hf($id);
        return redirect('admin/show');
    }

}
<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class user extends Model
{
    use SoftDeletes;
    protected $table = 'users';
    public $timestamps = false;
    public function add($data)
    {
     return $this->insert($data);
    }
    public function show($data)
    {
        return $this->where($data)->paginate(3);
    }
    public function del($id)
    {
        return $this->whereIn('id',$id)->delete();
    }
    public function upShow($id)
    {
        return $this->where('id',$id)->first();
    }
    public function upData($id,$data)
    {
        return $this->where('id',$id)->update($data);
    }

    public static function huishouzhan()
    {
        return self::onlyTrashed()->paginate(3);
    }
    public static function cd($id)
    {
        return self::onlyTrashed()->find($id)->forceDelete();
    }
    public static function hf($id)
    {
        return self::onlyTrashed()->find($id)->restore();
    }
}
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <form action="{{route('add')}}" method="post" enctype="multipart/form-data">
        @csrf

        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>

        <div class="form-group">
            <label for="email">上传图像</label>
            <input type="file" class="form-control" name="img">
        </div>

        <div class="form-group">
            <label for="email">真实姓名</label>
            <input type="text" class="form-control" name="uname">
        </div>

        <div class="form-group">
            <label for="email">昵称</label>
            <input type="text" class="form-control" name="name">
        </div>

        <div class="form-group">
            <label for="email">性别</label>
            <input type="text" class="form-control" name="sex">
        </div>

        <div class="form-group">
            <label for="email">手机号</label>
            <input type="text" class="form-control" name="tel">
        </div>

        <div class="form-group">
            <label for="email">出生日期</label>
            <input type="datetime-local" class="form-control" name="begintime">
        </div>

        <label for="email">状态</label>
        <div class="radio">
            <label><input type="radio" name="on" value="正常">正常</label>
            <label><input type="radio" name="on" value="不正常">不正常</label>
        </div>



        <div class="form-group">
            <label for="email">城市</label>
            <input type="text" class="form-control" name="city">
        </div>

        <div class="form-group">
            <label for="email">评论</label>
            <input type="text" class="form-control" name="text">
        </div>

        <div class="form-group">
            <label for="email">简介</label>
            <input type="text" class="form-control" name="utext">
        </div>
        <button type="submit" class="btn btn-primary">添加</button>
    </form>
</div>

</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <a href="{{ route('show') }}">回到展示页面</a>
    <table class="table">
        <thead>
        <tr>
            <th>ID</th>
            <th>真实姓名</th>
            <th>用户昵称</th>
            <th>性别</th>
            <th>用户头像</th>
            <th>状态</th>
            <th>手机号码</th>
            <th>所在地区</th>
            <th>设备类型</th>
            <th>用户来源</th>
            <th>注册时间</th>
            <th>功能操作</th>
        </tr>
        </thead>
        <tbody>
        @foreach($res as $v)
            <tr>
                <td>{{$v['id']}}</td>
                <td>{{$v['uname']}}</td>
                <td>{{$v['name']}}</td>
                <td>{{$v['sex']}}</td>
                <td><img src="{{ asset($v['img']) }}"  alt="" width="50px" height="50px"></td>
                <td>{{$v['on']}}</td>
                <td>{{$v['tel']}}</td>
                <td>{{$v['city']}}</td>
                <td>未知</td>
                <td>注册会员</td>
                <td>{{$v['begintime']}}</td>
                <td><a href="{{ route('cd') }}?id={{ $v['id'] }}">清除</a> <a href="{{ route('hf') }}?id={{ $v['id'] }}">恢复</a></td>

            </tr>
        @endforeach
        </tbody>
    </table>
    {{$res->links()}}
</div>

</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <form action="{{ route('show') }}" method="post">
        @csrf
        <input type="text" name="uname">
        <button type="submit">
            搜索
        </button>
    </form>
    <table class="table">
        <thead>
        <a href="{{ route('form') }}">添加</a>
        <button id="dellSure">批量删除</button>
        <tr>
            <th></th>
            <th>ID</th>
            <th>真实姓名</th>
            <th>用户昵称</th>
            <th>性别</th>
            <th>用户头像</th>
            <th>状态</th>
            <th>手机号码</th>
            <th>所在地区</th>
            <th>设备类型</th>
            <th>用户来源</th>
            <th>注册时间</th>
            <th>功能操作</th>
        </tr>
        </thead>
        <tbody>
        @foreach($res as $v)
        <tr>
            <td><input type="checkbox" class="checks" value="{{ $v['id'] }}"></td>
            <td>{{$v['id']}}</td>
            <td>{{$v['uname']}}</td>
            <td>{{$v['name']}}</td>
            <td>{{$v['sex']}}</td>
            <td><img src="{{ asset($v['img']) }}"  alt="" width="50px" height="50px"></td>
            <td class="dg">{{$v['on']}}</td>
            <td>{{$v['tel']}}</td>
            <td>{{$v['city']}}</td>
            <td>未知</td>
            <td>注册会员</td>
            <td>{{$v['begintime']}}</td>
            <td><a href="javascript:;" class="aaa" id="{{ $v['id'] }}">删除 </a>
                <a href="{{ route('upShow') }}?id={{ $v['id'] }}">修改</a></td>
            <td>
                <button>
                    <a href="{{ url('admin/huishouzhan') }}">回收站</a>
                </button>
            </td>
        </tr>
        @endforeach
        </tbody>
    </table>
    {{$res->appends($arr2)->links()}}
</div>

</body>
</html>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
    $('.aaa').click(function () {
        var id =$(this).attr('id');
        var _this=$(this);
        $.ajax({
            url:'del',
            data:{id:id},
            type:'get',
            dataType:'json',
            success:function (res) {
                if (res['code']=='200')
                _this.parents('tr').remove();
            }
        })
    })
    $('.dg').click(function () {
        if($(this).text()=='正常'){
            $(this).text()==$(this).text('不正常');
        }else{
            $(this).text()==$(this).text('正常');
        }
    })
    $('#dellSure').click(function (){
        var id=[];
        $('.checks:checked').each(function (){
            id.push($(this).val());
        })
        $.ajax({
            url:'del',
            data:{id:id},
            type:'get',
            dataType:'json',
            success:function (res) {
                if (res['code']=='200')
                    $('.checks:checked').each(function () {
                        $(this).parents('tr').remove()
                    })
            }
        })
    })
</script>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <form action="{{route('upData')}}?id={{ $res['id'] }}" method="post" enctype="multipart/form-data">
        @csrf
        <div class="form-group">
            <label for="email">上传图像</label>
            <input type="file" class="form-control" name="img">
        </div>

        <div class="form-group">
            <label for="email">真实姓名</label>
            <input type="text" class="form-control" name="uname" value="{{$res['uname']}}">
        </div>

        <div class="form-group">
            <label for="email">昵称</label>
            <input type="text" class="form-control" name="name" value="{{$res['name']}}">
        </div>

        <div class="form-group">
            <label for="email">性别</label>
            <input type="text" class="form-control" name="sex" value="{{$res['sex']}}">
        </div>

        <div class="form-group">
            <label for="email">手机号</label>
            <input type="text" class="form-control" name="tel" value="{{$res['tel']}}">
        </div>

        <div class="form-group">
            <label for="email">出生日期</label>
            <input type="datetime-local" class="form-control" name="begintime" value="{{$res['begintime']}}">
        </div>

        <label for="email">状态</label>
        <div class="radio">
            <label><input type="radio" name="on" value="正常">正常</label>
            <label><input type="radio" name="on" value="不正常">不正常</label>
        </div>



        <div class="form-group">
            <label for="email">城市</label>
            <input type="text" class="form-control" name="city" value="{{ $res['city'] }}">
        </div>

        <div class="form-group">
            <label for="email">评论</label>
            <input type="text" class="form-control" name="text" value="{{ $res['text'] }}">
        </div>

        <div class="form-group">
            <label for="email">简介</label>
            <input type="text" class="form-control" name="utext" value="{{ $res['utext'] }}">
        </div>
        <button type="submit" class="btn btn-primary">添加</button>
    </form>
</div>

</body>
</html>

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-11-09 19:14:44  更:2021-11-09 19:16:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/14 14:33:31-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码