1. 程式人生 > 其它 >Laravel-自帶分頁+搜尋

Laravel-自帶分頁+搜尋

public function getNewsList()
    {

        $condition = [];
        $cond = [];

        if (!empty($_GET['title'])) {
            array_push($condition, ["title", "like", "%{$_GET['title']}%"]);
            $cond['title'] = $_GET['title'];
        }
        $data = CaijiModel::where($condition)
            
->orderBy('id', 'desc') ->paginate(2); return view('caiji.index', compact('data', 'cond')); }
<?php

?>

    <!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>Document</title>
    <link rel="stylesheet" href="/css/bootstrap.css">
</head>
<body>

<form action="/caiji/getNewsList" method="get">
    <input type="text" name="title"><input type="submit" value="查詢">
</form>

<table border="1">
    <th>編號</th>
    <th>標題</th>
    <th>圖片</th>
    <th>作者</th>
    <th>瀏覽量</th>
    <th>點贊量</th>


    <?php foreach
($data as $key => $v): ?> <tr> <td><?php echo $v['id']; ?></td> <td><a href="/caiji/info/{{$v['id']}}"><?php echo $v['title']; ?></a></td> <td><img src="<?php echo $v['img']; ?>"/></td> <td><?php echo
$v['author']; ?></td> <td><?php echo $v['views']; ?></td> <td><?php echo $v['zan']; ?></td> </tr> <?php endforeach; ?> </table> {{$data->appends($cond)->links()}}