laravel框架简易增删(改)查

// 设置路由


//展示表单页面
Route::get('/week/add','weekController@add');
//添加
Route::post('/week/insert','weekController@insert');
//展示页面
Route::get('/week/index','weekController@index');
//删除页面
Route::get('/week/del/{id}','weekController@del');


//控制器页面
<?php

namespace App\Http\Controllers;

use App\weekModer;
use Illuminate\Http\Request;

class weekController extends Controller
{
//
public function add(){
return view('week');
}
public function insert(Request $request){
$parm=$request->except('_token');
// print_r($parm);
$this->validate($request,[
'name'=>'required',
'price'=>'required',
'type'=>'required',
'text'=>'required',
'time'=>'required'
],[
'name.required'=>'昵称不可以为空',
'price.required'=>'金额不可以为空',
'type.required'=>'类型不可以为空',
'text.required'=>'备注不可以为空',
'time.required'=>'创建时间不可以为空',
],$parm);
//5、添加入库前进行验证(2分)验证不通过不能入库(3分)
$res=weekModer::insert($parm);
//var_dump($res);
if ($res){
//6、实现页面的展示(2分)ORM或者DB方法不限,添加成功(3分)
echo "<script>alert('添加成功',location.href='/week/index')</script>";

}else{
echo "<script>alert('添加失败',location.href='/week/add')</script>>";
}


}

public static function index(){
$obj=weekModer::index();
//var_dump($obj);
return view('list',compact('obj'));


}

public static function del($id){
$res=weekModer::del($id);
// var_dump($res);
if ($res){
echo "<script>alert('删除成功',location.href='/week/index')</script>>";
}else{
echo "<script>alert('删除失败',location.href='/week/index')</script>>";
}

}


}

//模型
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class weekModer extends Model
{
//
protected $table='week';
public $primaryKey='id';
public $timestamps=false;

public static function insert($parm){
$obj=new self();
$obj->name=$parm['name'];
$obj->price=$parm['price'];
$obj->type=$parm['type'];
$obj->text=$parm['text'];
$obj->time=$parm['time'];
return $obj->save();

}

public static function index(){
return self::paginate(4);
}
public static function del($id){
return self::find($id)->delete();
}




}
//登录页面

 

 






<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
</head>
<body>
<form role="form" action="/week/insert" method="post" style="width: 350px">
@csrf
<div class="form-group">
<label for="name">昵称</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label for="name">金额</label>
<input type="text" class="form-control" name="price">
</div>
<div class="form-group">
<label for="name">类型</label>
<input type="text" class="form-control" name="type">
</div>
<div class="form-group">
<label for="name">备注</label>
<input type="text" class="form-control" name="text">
</div>
<div class="form-group">
<label for="name">创建时间</label>
<input type="text" class="form-control" name="time">
</div>
<button type="submit" class="btn btn-success">提交</button>

</form>
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</body>
</html>

列表展示页面

 

 

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>展示页面</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
</head>
<body>
<table class="table">
<tr>
<td>用户id</td>
<td>昵称</td>
<td>价格</td>
<td>类型</td>
<td>备注</td>
<td>创建时间</td>
<td>操作</td>
</tr>
@foreach($obj as $k=>$v)
<tr>
<td>{{$v->id}}</td>
<td>{{substr_replace("$v->name",'****',3,4)}}</td>
<td>
@if($v->name>=0)
<span style="color: green">{{$v->price}}</span>
@else
<span style="color: red">{{$v->price}}</span>
@endif

</td>
<td>{{$v->type}}</td>
<td>{{$v->text}}</td>
<td>{{$v->time}}</td>
<td>
<a href="/week/del/{{$v->id}}">删除</a>
{{--9、在页面添加操作一栏,实现页面中数据删除功能(2分)ORM或者DB方法不限,删除成功(3分)--}}
</td>
</tr>
@endforeach
</table>
{{$obj->links()}}
{{--7、在展示页面中添加分页效果(5分)--}}
</body>
</html>



























posted @ 2021-06-28 13:58  王越666  阅读(133)  评论(0编辑  收藏  举报