嗨,我正在尝试使用表单提交值以在数据库中进行搜索,但是我无法使其正常工作。有人可以帮忙吗?
<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
请帮助,非常感谢!
答案 0 :(得分:0)
通过在表单中添加csrf令牌来修复异常
<form action="/comp1230/assignments/assignment2/public/search" method="post">
@csrf
答案 1 :(得分:0)
Laravel分页仅适用于get参数。
在分页之后,数组转换需要其他概念
以下示例说明了这一概念
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');