无法使用 laravel 在 mongodb 数据库中搜索数据
Unable to search data in a mongodb database using laravel
我正在尝试编写查询以在 mongodb 数据库中搜索特定查询,并且 return 这里的相关信息是我目前所做的:
在 Post:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Jenssegers\Mongodb\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $connection = 'mongodb';
protected $collection = 'post';
protected $fillable = [
'STATUS',
'DDC_CODE',
'TRADE_NAME',
'SCIENTIFIC_CODE',
'SCIENTIFIC_NAME',
'INGREDIENT_STRENGTH',
'DOSAGE_FORM_PACKAGE',
'ROUTE_OF_ADMIN',
'PACKAGE_PRICE',
'GRANULAR_UNIT',
'MANUFACTURER',
'REGISTERED_OWNER',
'UPDATED_DATE',
'SOURCE'
];}
在Post控制器中:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function home() {
$posts = Post::paginate(4);
return view('home', compact('posts'));
}
public function search(Request $request){
if(isset($_GET['query'])){
$search_text=$_GET['query'];
$searchs=Post::whereRaw(array('$text'=>array('$search'=> $search_text)))->get();;
return view('search',['searchs'=>$searchs]);
}
else{
return view('search');
}
}
}
和 search.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Laravel search</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6" style="margin-top:40px">
<h4>Search Everything</h4><hr>
<form action="{{ route('web.search')}}" method="GET">
<div class="form-group">
<label for="">Enter keyword</label>
<input type="text" class="form-control" name="query" placeholder="Search here...">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</form>
<br>
<br>
<hr>
<br>
<table border = "1">
<tr>
<td>STATUS</td>
<td>DDC_CODE</td>
<td>TRADE_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>SCIENTIFIC_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>INGREDIENT_STRENGTH</td>
<td>DOSAGE_FORM_PACKAGE</td>
<td>ROUTE_OF_ADMIN</td>
<td>PACKAGE_PRICE</td>
<td>GRANULAR_UNIT</td>
<td>MANUFACTURER</td>
<td>UPDATED_DATE</td>
<td>SOURCE</td>
</tr>
@foreach ($searchs as $search)
<tr>
<td>{{ $search['STATUS'] }}</td>
<td>{{ $search['DDC_CODE'] }}</td>
<td>{{ $search['TRADE_NAME'] }}</td>
<td>{{ $search['SCIENTIFIC_CODE'] }}</td>
<td>{{ $search['SCIENTIFIC_NAME'] }}</td>
<td>{{ $search['INGREDIENT_STRENGTH'] }}</td>
<td>{{ $search['DOSAGE_FORM_PACKAGE'] }}</td>
<td>{{ $search['ROUTE_OF_ADMIN'] }}</td>
<td>{{ $search['PACKAGE_PRICE'] }}</td>
<td>{{ $search['GRANULAR_UNIT'] }}</td>
<td>{{ $search['MANUFACTURER'] }}</td>
<td>{{ $search['REGISTERED_OWNER'] }}</td>
<td>{{ $search['UPDATED_DATE'] }}</td>
<td>{{ $search['SOURCE'] }}</td>
</tr>
@endforeach
</table>
</body>
</html>
还有路由处理程序web.php:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/', function () {
return view('welcome');
});
Route::get('view','App\Http\Controllers\PostController@home');
Route::get('/search',[PostController::class,'search'])-
>name('web.search');
当我 运行 代码出于某种原因它给了我一个 MongoDB\Driver\Exception\CommandException text index required for $text query
使用的查找方法可能是错误的正确的选择是什么?连接本身也没有问题,因为 postController 运行 中的 home 方法是完美的。感谢您提供任何帮助。
当您在没有查询的情况下调用页面时,缺少 $searchs 变量。您要么必须 return else 分支中的 search-view 并设置 $searchs 变量:
// ...
else {
return view('search',['searchs' => []);
}
或在您的 blade 文件中添加条件:
@if(isset($searchs))
@foreach($searchs as $search)
我正在尝试编写查询以在 mongodb 数据库中搜索特定查询,并且 return 这里的相关信息是我目前所做的: 在 Post:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Jenssegers\Mongodb\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $connection = 'mongodb';
protected $collection = 'post';
protected $fillable = [
'STATUS',
'DDC_CODE',
'TRADE_NAME',
'SCIENTIFIC_CODE',
'SCIENTIFIC_NAME',
'INGREDIENT_STRENGTH',
'DOSAGE_FORM_PACKAGE',
'ROUTE_OF_ADMIN',
'PACKAGE_PRICE',
'GRANULAR_UNIT',
'MANUFACTURER',
'REGISTERED_OWNER',
'UPDATED_DATE',
'SOURCE'
];}
在Post控制器中:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function home() {
$posts = Post::paginate(4);
return view('home', compact('posts'));
}
public function search(Request $request){
if(isset($_GET['query'])){
$search_text=$_GET['query'];
$searchs=Post::whereRaw(array('$text'=>array('$search'=> $search_text)))->get();;
return view('search',['searchs'=>$searchs]);
}
else{
return view('search');
}
}
}
和 search.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Laravel search</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6" style="margin-top:40px">
<h4>Search Everything</h4><hr>
<form action="{{ route('web.search')}}" method="GET">
<div class="form-group">
<label for="">Enter keyword</label>
<input type="text" class="form-control" name="query" placeholder="Search here...">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</form>
<br>
<br>
<hr>
<br>
<table border = "1">
<tr>
<td>STATUS</td>
<td>DDC_CODE</td>
<td>TRADE_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>SCIENTIFIC_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>INGREDIENT_STRENGTH</td>
<td>DOSAGE_FORM_PACKAGE</td>
<td>ROUTE_OF_ADMIN</td>
<td>PACKAGE_PRICE</td>
<td>GRANULAR_UNIT</td>
<td>MANUFACTURER</td>
<td>UPDATED_DATE</td>
<td>SOURCE</td>
</tr>
@foreach ($searchs as $search)
<tr>
<td>{{ $search['STATUS'] }}</td>
<td>{{ $search['DDC_CODE'] }}</td>
<td>{{ $search['TRADE_NAME'] }}</td>
<td>{{ $search['SCIENTIFIC_CODE'] }}</td>
<td>{{ $search['SCIENTIFIC_NAME'] }}</td>
<td>{{ $search['INGREDIENT_STRENGTH'] }}</td>
<td>{{ $search['DOSAGE_FORM_PACKAGE'] }}</td>
<td>{{ $search['ROUTE_OF_ADMIN'] }}</td>
<td>{{ $search['PACKAGE_PRICE'] }}</td>
<td>{{ $search['GRANULAR_UNIT'] }}</td>
<td>{{ $search['MANUFACTURER'] }}</td>
<td>{{ $search['REGISTERED_OWNER'] }}</td>
<td>{{ $search['UPDATED_DATE'] }}</td>
<td>{{ $search['SOURCE'] }}</td>
</tr>
@endforeach
</table>
</body>
</html>
还有路由处理程序web.php:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/', function () {
return view('welcome');
});
Route::get('view','App\Http\Controllers\PostController@home');
Route::get('/search',[PostController::class,'search'])-
>name('web.search');
当我 运行 代码出于某种原因它给了我一个 MongoDB\Driver\Exception\CommandException text index required for $text query
使用的查找方法可能是错误的正确的选择是什么?连接本身也没有问题,因为 postController 运行 中的 home 方法是完美的。感谢您提供任何帮助。
当您在没有查询的情况下调用页面时,缺少 $searchs 变量。您要么必须 return else 分支中的 search-view 并设置 $searchs 变量:
// ...
else {
return view('search',['searchs' => []);
}
或在您的 blade 文件中添加条件:
@if(isset($searchs))
@foreach($searchs as $search)