///////////////////////////////////////////////////路由
//展示表单页面
Route::get('/movies/add','moviesController@add');
//表单提交页面
Route::post('/movies/insert','moviesController@insert');
//列表展示页面
Route::get('/movies/index','moviesController@index');
//删除
Route::get('/movies/del/{id}','moviesController@del');
//修改展示页面
Route::get('/movies/updata/{id}','moviesController@updataselect');
//修改
Route::post('/movies/updatas','moviesController@updatas');
////////////////////////////////////////////表单添加页面
<!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="/movies/insert" method="post"enctype="multipart/form-data" style="width: 300px;">
@csrf
<div class="form-group">
<label for="name">电影封面</label>
<input type="file" class="form-control" name="imgs">
</div>
<div class="form-group">
<label for="name">电影简介</label>
<input type="text" class="form-control" name="name">
</div>
<input type="submit" value="立即添加">
</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>
//////////////////////////////////////laravel 框架控制器页面
<?php
namespace App\Http\Controllers;
use App\models\moviesModel;
use Illuminate\Http\Request;
class moviesController extends Controller
{
public function add()
{
return view('movies.movies');
}
public function insert(Request $request)
{
$param = $request->all();
// var_dump($param);
$this->validate($request, [
'imgs' => 'required|image',
'name' => 'required',
], [
'imgs.required' => '电影封面不可以为空',
'name.required' => '电影名称不可以为空',
'imgs.image' => '电影封面类型为JPG',
], $param);
////处理图片
$path = $request->imgs->store('moviesimgs');
$param['imgs'] = '/' . $path;
$result = moviesModel::insert($param);
// var_dump($result);
if ($result) {
echo "<span style='color: red'>添加成功</span>";
header('refresh:3,url=/movies/index');
} else {
echo "<span style='color: red'>添加失败/span>";
header('refresh:3,url=/movies/add');
}
}
public function index(Request $request)
{
$word=$request->input('word');
// var_dump($word);
//进行非空判断
$arr=[];
if (!empty($word)){
$arr['name']=$word;
}
if ($word){
foreach ($arr as $k=>$v){
// print_r($v);
//$arr[$k]=str_replace($word,"<font color='red'>$word</font>",$v);
}
}
$res = moviesModel::index($arr);
//var_dump($res);
return view('movies.movieslist', compact('res'));
}
public function del($id)
{
$res = moviesModel::del($id);
if ($res) {
echo '删除成功';
header('refresh:2,url=/movies/index');
}else{
echo '删除失败';
header('refresh:2,url=/movies/index');
}
}
public function updataselect($id){
// print_r($id);
//将模型里的方法进行调用
$result=moviesModel::updata($id);
// var_dump($result);
return view(' movies.moviesselect',compact('result'));
}
public function updatas(Request $request){
$params=$request->all();
// print_r($params);
$res=moviesModel::updata($params);
//处理图片的添加
$path=$request->imgs->store('moviesimgs');
$params['imgs']='/'.$path;
$res=moviesModel::updatas($params);
// var_dump($res);
if ($res) {
echo "<span style='color: red'>修改成功</span>";
header('refresh:2,url=/movies/index');
}else{
echo "<span style='color: red'>修改失败</span>";
header('refresh:2,url=/movies/index');
}
}
}////////////////////////////////////摸型页面
<?php
namespace App\models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class moviesModel extends Model
{
//
use SoftDeletes;
protected $table = 'movies';
public $primaryKey = 'id';
public $timestamps = false;
public static function insert($param)
{
$obj = new self();
$obj->imgs = $param['imgs'];
$obj->name = $param['name'];
return $obj->save();
}
public static function index($arr)
{
// var_dump($arr);
$obj = new self();
if (isset($arr['name'])) {
$obj = $obj->where('name', 'like', '%' . $arr['name'] . '%');
}
$data = $obj->orderBy('id', 'desc')
->paginate(2);
return $data;
}
//删除
public static function del($id)
{
return self::find($id)->delete();
}
//修改表单页面
public static function updata($id)
{
return self::find($id);
}
public static function updatas($params){
$objest= self::find($params['updata_id']);
$objest->imgs=$params['imgs'];
$objest->name=$params['name'];
return $objest->save();
}
}
////////////////////////////////网页列表展示页面
<!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>
<p><input type="button" value="返回上一级页面" class="btn btn-danger" onclick="back()">
<input type="button" value="返回下一级页面" class="btn btn-danger" onclick="go()">
</p>
<a href="/movies/add"> <span style="color: red">点击此处返回添加页面</span> </a>
<form action="/movies/index" method="get">
<input type="text" placeholder="请输入电影标题进行查找" name="word" >
<input type="submit" value="立即搜索" class="btn btn-info">
</form>
<table class="table">
<tr>
<td>id</td>
<td>电影封面</td>
<td>电影标题</td>
<td>操作</td>
</tr>
@foreach($res as $k=>$v)
<tr>
<td>{{$v->id}}</td>
<td><img src="{{$v->imgs}}" alt="" width="100px" height="100px"></td>
<td>{{$v->name}}</td>
<td>
<a href="/movies/del/{{$v->id}}" onclick=" return confirm('您确定要删除吗?')">删除</a>
<a href="/movies/updata/{{$v->id}}" onclick="updata()">修改</a>
</td>
</tr>
@endforeach
</table>
{{$res->links()}}
</body>
</html>
<script src="jquery-1.10.2.min.js"></script>
<script>
function updata(){
confirm('您确定要修改吗?')
}
function back(){
window.history.go(-1)
}
function go(){
window.history.go(1)
}
</script>
/////////////////////////////////////////////////修改页面
<!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="/movies/updatas" method="post"enctype="multipart/form-data" style="width: 300px;">
@csrf
<div class="form-group">
<label for="name">电影封面</label>
<input type="file" class="form-control" name="imgs">
<img src="{{$result->imgs}}" alt="" width="100px" height="100px">
</div>
<div class="form-group">
<label for="name">电影简介</label>
<input type="text" class="form-control" name="name" value="{{$result->name}}">
</div>
<input type="hidden" value="{{$result->id}}" name="updata_id">
<input type="submit" value="立即修改" class="btn btn-info">
</form>
</body>
</html>