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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值