专业四简易博客系统(搜索、软删除,资源路由)

//////////////////////////////博客系统路由
Route::group(['prefix'=>'admin'],function (){
//展示表单添加页面
Route::resource('img','imgController');
//添加
Route::resource('insert','imgController');
//删除
Route::get('/del/{id}','imgController@del');
});

 

 

///////////////////////////////////////////添加页面

<!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="/admin/insert" method="post" enctype="multipart/form-data">
@csrf
姓名<input type="text" name="name"><br>
性别 <input type="radio" name="sex" value="1" checked>男
<input type="radio" name="sex" value="2">女<br>
照片 <input type="file" name="imgs" ><br>
<input type="submit" value="立即添加"><br>
</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>

 

 //////////////////////////////////////////////////////控制器页面

<?php

namespace App\Http\Controllers;

use App\modelWeek;
use Illuminate\Http\Request;


class imgController extends Controller
{

public function index(Request $request)
{
//进行模糊查询
$word = $request->input('word');
// print $word;
$where = [];
if (!empty($word)) {
$where['name'] = $word;
}
// print_r($where);
//列表展示
$res = modelWeek::index($where);
// var_dump($res);
return view('img.list', compact('res'));
}


public function create()
{
//添加,也就是展示的表单的页面

return view('img.img');
}


public function store(Request $request)
{
//处理添加的方法
$param = $request->all();
$this->validate($request, [
"name" => "required",
"sex" => "required",
"imgs" => "required|image",
], [
"name.required" => '姓名不可以为空',
"sex.required" => '性别不可以为空',
"imgs.required" => '学生照片不可以为空',
"imgs.image" => '学生照片类型为jpg',

], $param);
//处理图片
$path = $request->imgs->store('imgs');
$param['imgs'] = "/" . $path;
$res = modelWeek::insert($param);
//var_dump($res);
if ($res) {
echo "添加成功";
header('refresh:3,url=insert');


} else {
echo "添加失败";
header('refresh:3,url=insert');
}
}

public function show($id)
{


}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}


public function update(Request $request, $id)
{
//
}


public function destroy($id)
{
//删除
$res = modelWeek::destroy($id);
var_dump($res);

}

public function del($id)
{
$res = modelWeek::del($id);
/// var_dump($res);
if ($res) {
echo "删除成功";
header('refresh:3,url=/admin/insert');
} else {
echo "删除失败";
header('refresh:3,url=/admin/insert');
}

}



}
///////////////////////////////////列表展示页面
<!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="/admin/insert" method="get">
<input type="text" name="word">
<input type="submit" value="立即搜索">
</form>
<table class="table">
<tr>
<td>id</td>
<td>学生姓名</td>
<td>学生性别</td>
<td>学生照片</td>
<td>操作</td>
</tr>
@foreach($res as $k=>$v)
<tr>
<td>{{$v->id}}</td>
<td>{{$v->name}}</td>
<td>
@if($v->sex==1)
<span style="color: red">男</span>
@else
<span style="color: green">女</span>
@endif
</td>
<td><img src="{{$v->imgs}}" alt="" style="width: 100px" height="100px"></td>
<td>
<a href="/admin/del/{{$v->id}}">删除</a>
<a href="/admin/insert/{{$v->id}} ">修改</a>
</td>
</tr>
@endforeach
</table>
{{$res->links()}}
</body>

</html>

///////////////////////////////////////////////模型页面
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use Illuminate\Database\Eloquent\SoftDeletes;

//软删除第一步在数据库中添加deleted_at
//软删除第二步引用这个方法use Illuminate\Database\Eloquent\SoftDeletes;
class modelWeek extends Model
{
//软删除第三步,在模型里使用这个方法
use SoftDeletes;

protected $table = 'imgs';
public $timestamps = false;
public $PrimaryKey = 'id';

//添加
public static function insert($Param)
{
$obj = new self();
$obj->name = $Param['name'];
$obj->sex = $Param['sex'];
$obj->imgs = $Param['imgs'];
return $obj->save();

}

//展示
public static function index($where)
{
$obj = new self();
if (isset($where['name'])) {
$obj=$obj->where('name', 'like', '%'.$where['name'].'%');
}
$data= $obj->orderBy('id', 'desc')
->paginate(2);
return $data;

//
// return self::orderBy('id', 'desc')
// ->paginate(2);

}

//删除
public static function del($id)
{
return self::find($id)->delete();


}


}















 

posted @ 2021-07-08 09:42  王越666  阅读(47)  评论(0编辑  收藏  举报