1. 程式人生 > 其它 >laravel 框架簡易增刪改查

laravel 框架簡易增刪改查

參看網址:http://www.yan.com/mou/add

圖書增加HTML頁面

//圖書增加路由
Route::get('mou/add','MouController@store');
//控制器程式碼,這一步渲染新增的檢視
public function add() { return view('mou.mouadd'); }

//新增檢視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="/mou/insert" method="post" enctype="multipart/form-data" style="width: 300px;"> @csrf <div class="form-group"> <label for="name">標題</label> <input type="text" class="form-control" name="title" > </div> <div class
="form-group"> <label for="name">作者</label> <input type="text" class="form-control" name="author" > </div> <div class="form-group"> <label for="name">書圖</label> <input type="file" class="form-control" name="img" > </div> <div class="form-group"> <label for="name">分類</label> <input type="text" class="form-control" name="type" > </div> <input type="submit" value="立即新增"> </form> </body> </html>

//處理新增入庫,資料庫展示,刪除,修改以及靜態化詳情控制器程式碼

 
<?php

namespace App\Http\Controllers;

use App\models\mouModel;
use Illuminate\Http\Request;
use QL\QueryList;

class MouController extends Controller
{
    //
    public function store()
    {
        //採集的地址
        $url = 'https://www.hongxiu.com/search?kw=%E5%8E%86%E5%8F%B2';
        $content = file_get_contents($url);

        $range = '.book-mid-info';
        $rules = [
            'title' => ['h4', 'text'],
            'author' => ['p[class=author]', 'text'],
            'img' => ['img', 'src'],
            'type' => ['a[target=_blank]', 'text']
        ];
        $data = QueryList::html($content)
            ->range($range)
            ->rules($rules)
            ->queryData();

        foreach ($data as $k => $v) {
            $path = 'http:' . $v['img'];
            $file = file_get_contents($path);
            $filename = './book/' . md5($k) . '.jpg';
            file_put_contents($filename, $file);
        }

        $res = mouModel::store($data);
        if ($res) {
            echo "<font color='red'>採集成功</font>";
            header('refresh:2,url=/mou/list');
        } else {
            echo "<font color='red'>採集失敗</font>";
            header('refresh:2,url=/mou/list');
        }
    }
//圖書新增頁面
    public function add()
    {
        return view('mou.mouadd');
    }
//圖書入庫
public function insert(Request $request) { // 接受引數 $params = $request->except('_token'); //處理圖片 $path = '/' . $request->file('img')->store('img'); $params['img'] = $path; // 進行新增入庫 $res = mouModel::store($params); if ($res) { echo "<font color='red'>新增成功</font>"; header('refresh:2,url=/mou/list'); } else { echo "<font color='red'>新增失敗</font>"; header('refresh:2,url=/mou/add'); } } //展示 public function list(Request $request) { $word=$request->input('word'); $data = mouModel::list($word); return view('mou.moulist', compact('data','word')); } //刪除 public function del($id) { // 這裡可進行驗證id不可以為空,是不是純數字 $res = mouModel::del($id); if ($res) { echo "<font color='red'>刪除成功</font>"; header('refresh:2,url=/mou/list'); } else { echo "<font color='red'>刪除失敗</font>"; header('refresh:2,url=/mou/list'); } } //詳情展示 public function updataone($id) { // 這裡可進行驗證id不可以為空,是不是純數字 $data = mouModel::updataone($id); return view('mou.mouupdataone', compact('data')); } //修改 public function updata(Request $request) { $params = $request->all(); $res = mouModel::updata($params); if ($res) { echo "<font color='red'>修改成功</font>"; header('refresh:2,url=/mou/list'); } else { echo "<font color='red'>修改失敗</font>"; header('refresh:2,url=/mou/list'); } } //靜態化詳情 public function listone($id) { $filrname = "./book/" . md5($id) . ".html"; if (file_exists($filrname)) { echo file_get_contents($filrname); } else { $data = mouModel::listone($id); $html = view('mou.moulistone', compact('data')); file_put_contents($filrname, $html); return $html; } } }

//處理新增入庫,資料庫展示,刪除,修改以及靜態化詳情模型程式碼

<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class mouModel extends Model
{
    //軟刪除
    use SoftDeletes;
//定義資料表
    protected $table = 'mou';
//    定義主鍵id
    public $primaryKey = 'id';
//    設定時間戳為關
    public $timestamps = false;
//新增入庫的模型程式碼
    public static function store($param)
    {
        return self::insert($param);
    }
    //展示
    public static function list($word)
    {
        return self::where('title','like',"%$word%")->paginate(5);

    }
    //刪除
    public static function del($id)
    {
        return self::find($id)->delete();
    }
    //修改展示頁面
    public static function updataone($id)
    {
        return self::find($id);
    }
    //修改
    public static function updata($params)
    {
        $obj = self::find($params['updata_id']);
        $obj->title = $params['title'];
        $obj->author = $params['author'];
        $obj->img = $params['new_img'];
        $obj->type = $params['type'];
        return $obj->save();
    }
    //詳情展示
    public static function listone($id){
        return self::find($id);
    }
}

//修改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="/mou/updata" method="post" style="width: 300px;">
    @csrf
    <div class="form-group">
        <label for="name">標題</label>
        <input type="text" class="form-control" name="title" value="{{$data['title']}}" >
    </div>
    <div class="form-group">
        <label for="name">作者</label>
        <input type="text" class="form-control" name="author" value="{{$data['author']}}" >
    </div>
    <div class="form-group">
        <label for="name">書圖</label>
        <img src="{{$data['img']}}" alt="無法顯示">
        <input type="file" value="new_img" name="new_img" >
    </div>
    <div class="form-group">
        <label for="name">分類</label>
        <input type="text" class="form-control" name="type" value="{{$data['type']}}">
    </div>
{{--    隱藏的id,傳至控制器--}}
    <input type="hidden" value="{{$data['id']}}" name="updata_id">
    <input type="submit" value="立即修改">
</form>

</body>
</html>