专业四 博客简易界面(增删改查)
<!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>
<form action="/bk/insert" method="post" enctype="multipart/form-data" style="width: 350px">
@csrf
<p>
博客标题
<input type="text" name="title" class="form-control">
</p>
<p>
博客价格
<input type="text" name="price" class="form-control">
</p>
<p>
博客图片
<input type="file" name="imgs" class="form-control">
</p>
<input type="submit" value="立即注册" class="btn btn-success">
</form>
</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>博客的标题</td>
<td>博客的价格</td>
<td>博客的图片</td>
<td>操作</td>
</tr>
@foreach($result as $k=>$v)
<tr>
<td>{{$v->title}}</td>
<td>
<span style="color: orangered">{{$v->price}}</span>
</td>
<td><img src="{{$v->imgs}}" alt="无法显示" style="width: 100px" height="100px"></td>
<td>
<a href="/bk/del/{{$v->id}}" onclick=" return confirm('您确定要删除吗?') ">删除</a>
<a href="/bk/selectone/{{$v->id}}">修改</a>
</td>
</tr>
@endforeach
</table>
{{$result->links()}}
<p>本页共有 <span style="color: red">{{$result->count()}}</span>数据 </p>
</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>
<form action="/bk/updata" method="post" enctype="multipart/form-data">
@csrf
<p>
博客标题
<input type="text" name="title" class="form-control" value="{{$result->title}}">
</p>
<p>
博客价格
<input type="text" name="price" class="form-control" value="{{$result->price}}">
</p>
<p>
博客图片
<input type="file" name="imgs" class="form-control" value="{{$result->imgs}}">
</p>
<input type="hidden" name="bk_id" value="{{$result->id}}" >
<input type="submit" value="立即修改" class="btn btn-success">
</form>
</body>
</html>
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、PHP路由页面
//博客
//展是表单页面
Route::get('/bk/add','bkController@add');
//接受添加数据
Route::post('/bk/insert','bkController@insert');
//展是页面
Route::get('/bk/index','bkController@index');
//展示删除功能
Route::get('/bk/del/{id}','bkController@del');
//展示详情页面(展示修改的页面)
Route::get('/bk/selectone/{id}','bkController@selectone');
//修改页面
Route::post('/bk/updata','bkController@updata');
//php控制器页面
<?php
namespace App\Http\Controllers;
use App\models\bkModel;
use Illuminate\Http\Request;
class bkController extends Controller
{
public function add(){
return view('bk.bk');
}
public function insert(Request $request){
$parm=$request->all();
$this->validate($request,[
'title'=>'required',
'price'=>'required',
'imgs'=>'required',
],[
'title.request'=>'标题不可以为空',
'price.request'=>'价格不可以为空',
'imgs.request'=>'图片不可以为空',
],$parm);
// echo "ok";die;
$path=$request->imgs->store('images');
$parm['imgs']='/'.$path;
$result=bkModel::insert($parm);
if ($result){
echo "<span style='color: green' >添加成功</span>";
header('refresh:2,url=/bk/index');
}else{
echo "<span style='color: red'>添加失败</span>";
header('refresh:2,url=/bk/add');
}
}
/////////////////////////////////////////table展示页面
public function index(){
$result=bkModel::index();
return view('bk.bklist',compact('result'));
}
//删除页面
public function del($id){
$result=bkModel::del($id);
// var_dump($result);die;
if ($result){
echo "<span style='color: green' >删除成功</span>";
header('refresh:2,url=/bk/index');
}else{
echo "<span style='color: red'>删除失败</span>";
header('refresh:2,url=/bk/index');
}
}
//修改的方法
public function selectone($id){
$result=bkModel::selectone($id);
return view('bk.bkselectone',compact('result'));
}
//修改
public function updata(Request $request){
$parm=$request->all();
// var_dump($parm);
$path=$request->imgs->store('images');
$parm['imgs']='/'.$path;
$res=bkModel::updata($parm);
// var_dump($res);
if ($res){
echo "<span style='color: green' >修改成功</span>";
header('refresh:2,url=/bk/index');
}else{
echo "<span style='color: green' >修改失败</span>";
header('refresh:2,url=/bk/index');
}
}/////////////////////////////////php模型页面
<?php
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class bkModel extends Model
{
//模型与表的关系
protected $table='bk';
public $timestamps=false;
public $primaryKey='id';
public static function insert($parm){
$obj=new self();
$obj->title=$parm['title'];
$obj->price=$parm['price'];
$obj->imgs=$parm['imgs'];
return $obj->save();
}
//展示列表的方法
public static function index(){
return self::paginate(3);
}
//删除数据的方法
public static function del($id){
return self::find($id)->delete();
}
//根据控制器传过来的id去数据库提取数据返回至控制器,进行页面展示数据,也就是修改的页面
public static function selectone($id){
return self::find($id);
}
public static function updata($parm){
$obj=self::find($parm['bk_id']);
$obj->title=$parm['title'];
$obj->price=$parm['price'];
$obj->imgs=$parm['imgs'];
return $obj->save();
}
}
}