Laravel 检查表单提交值

问题描述 投票:0回答:2

嗨,我试图使用表单提交一个值以在数据库中搜索,但是我无法使其工作。有人可以帮忙吗?

<form action="/comp1230/assignments/assignment2/public/search" method="post">
<h2>Please enter the keywords you want to search:</h2>
<input id="search"type=text name="searchcontent">
<input type="submit" value='Search'>
</form>

上面是welcome.blade.php表单,我想在我的控制器中使用这个提交的值,代码如下所示:

    public function search()
{

     $search = Input::get('searchcontent');
     $results=Records::paginate(5);
     $records=[];
     foreach($results as $result)
     if(!in_array($search,$result)){
        continue;
     }else{
        array_push($records,$result);
     }

    return view('home',['records'=>$records]);
}  

路线:

Route::post('/search', 'RecordController@search');

但是我得到的是

419
Sorry, your session has expired. Please refresh and try again.

GO HOME

请帮忙,非常感谢!

php laravel forms controller submit
2个回答
0
投票

Laravel 分页仅适用于获取参数。

分页之后,数组转换需要各种概念

下面的例子说明了这个概念

          1. Get data from database
          2. Filter data corresponding search_content using in_array (in_array accepts data array only and not for objects)
          3. Perform manual pagination and display records

RecordsController.php 代码

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;

use App\Records;

class RecordController extends Controller
{
    // Show Entry Form
    public function index()
    {
        return view('search_entry');

    }

    public function search(Request $request)
    {

         $search = Input::get('searchcontent');

         $results=Records::get()->toArray();
         $records=[];

         foreach($results as $result)       //in_array accepts array only
         {

            if(in_array($search, $result))
            {
                array_push($records, $result);
            }

         }

        $page = $request->page; // current page for pagination

        // manually slice array of product to display on page
        $perPage = 5;
        $offset = ($page-1) * $perPage;
        $new_record = array_slice($records, $offset, $perPage);

        // your pagination 
        $new_record = new Paginator($new_record, count($records), $perPage, $page, ['path'  => $request->url(),'query' => $request->query()]);

        return view('home',['records' => $new_record]);
    }  
}

search_entry.blade.php 代码

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>LaravelTest</title>
    </head>
    <body>
        <form action="{{url('search')}}" >
            <h2>Please enter the keywords you want to search:</h2>
            <input id="search"type=text name="searchcontent">
            <input type="submit" value='Search'>
        </form>


    </body>
</html>

home.blade.php 代码(结果视图 php)

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>LaravelTest</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
    </head>
    <body>
        <h3>SEARCH CONTENT</h3>
        <table border=1 cellspacing=0 cellpadding=2>
            <thead>
                <tr>
                    <td>ID</td> <td>NAME</td> <td>AGE</td>
                </tr>
            </thead>
            <tbody>
                <?php
                    foreach($records as $record)
                    {
                        echo "<tr><td>".$record['id']."</td><td>".$record['name']."</td><td>".$record['age']."</td></tr>";
                    }
                ?>
            </tbody>
            </table>
            {{ $records->links('pagination::bootstrap-4') }}


    </body>
</html>

在Web.php中添加以下路由代码

Route::get('search_entry','RecordController@index');
Route::get('search','RecordController@search');

0
投票

通过将 csrf 令牌添加到表单来修复异常

<form action="/comp1230/assignments/assignment2/public/search" method="post">
    @csrf
© www.soinside.com 2019 - 2024. All rights reserved.