1. 程式人生 > 其它 >專業四 部落格簡易介面(增刪改查)

專業四 部落格簡易介面(增刪改查)

<!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();
}




}

}