Home > Software engineering >  Unable to search data in a mongodb database using laravel
Unable to search data in a mongodb database using laravel

Time:05-19

I am trying to write a query to search the mongodb database for a specific query and return the associated information here is what I have done so far: In 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'
];}

In PostController:

<?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');
    }
 }
}

And the 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 >
        <div >
            <div  style="margin-top:40px">
            <h4>Search Everything</h4><hr>
            <form action="{{ route('web.search')}}" method="GET">
                <div >
                    <label for="">Enter keyword</label>
                    <input type="text"  name="query" placeholder="Search here...">
                </div>
                <div >
                    <button type="submit" >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>

Also the route handler 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');

When I run the code for some reason it gives me a MongoDB\Driver\Exception\CommandException text index required for $text query The find method used is probably wrong what is the correct alternative? Also there is no problems with the connection itself as the home method in the postController runs flawlessly. Any and all help is appreciated.

CodePudding user response:

The $searchs variable is missing, when you call the page with no query. You either have to return the search-view in the else branch with the $searchs variable set:

// ...
 else {
        return view('search',['searchs' => []);
 }

or put a condition in your blade file:

@if(isset($searchs))
   @foreach($searchs as $search)
  • Related