PHP/Laravel search query controller

I am trying to build a search query to use the user's search input (gene name) and return the gene location and symbol from a JSON file I loaded in.

My Site Controller for the JSON input:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        $results = file_get_contents("http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json");
        $data = json_decode($results, true);
        dd($data);
    }
}

My Search Controller for the search function:

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SearchController extends Controller
{
    public function search(Request $request){
    // Get the search value from the request
    $search = $request->input('search');

    // Search in the name column from the data table
    $data = data::query()
        ->where('name', 'LIKE', "%{$search}%")
        ->get();

    // Return the search view with the results compacted
    return view('search', compact('data'));
    }
}

My routes:

<?php

use Illuminate\Support\Facades\Route;
//use App\Http\Controllers\Controller;
use App\Http\Controllers\SiteController;

Route::get('/data', [SiteController::class, 'index']);
Route::get('search', [SearchController::class, 'search']);
Route::get('/', function () {
    return view('welcome');
});

And my welcomeblade:

    <!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Geisinger</title>

       <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<script src="https://kit.fontawesome.com/1866062af8.js" crossorigin="anonymous"></script>

             <link rel="stylesheet" type="text/css" href="{{ asset('/styles.css') }}" >
    </head>

 <body>
     <img src="{{ asset('/Geisinger_logo.jpg') }}" alt="Geisinger Logo">
    <h1> Geisinger Gene Search </h1>

     <div class="container my-8 py-5 px-5 mx-5">
    <!-- Search input -->
    <form action="{{ route('search') }}" method="GET">>
        <input type="search" class="form-control" placeholder="Search Gene Name" name="search">
        <button type="submit">Search</button>
    </form>
    <div class="d-grid gap-2 col-6 mx-auto">
  <button class="btn btn-outline-dark" type="button">Search <i class="fa-solid fa-magnifying-glass"></i></button>
</div>
<br>
<br>

<h2>Results <i class="fa-solid fa-square-poll-horizontal"></i></h2>
    <!-- List items -->
   <?php
    @if($data->isNotEmpty())
    @foreach ($data as $data)
        <ul class="list-group mt-3">
        <li class="list-group-item">{{ $data->name }}<</li>
        <li class="list-group-item">{{ $data->location }}</li>
        <li class="list-group-item">{{ $data->symbol }}</li>
    </ul>
    @endforeach
@else 
    <div>
        <h2>No Gene found</h2>
    </div>
@endif
   ?> 



    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
    </body>
</html>

I keep getting "route 'search' not defined" and "syntax error unexpected token "if"". Thank you for your help!

Answer

Solution:

You can only access the route() method on named routes. In your web.php file, you must give your search route the name search before you can access it via route('search')

In you web.php change:

Route::get('search', [SearchController::class, 'search']);

to this:

Route::get('search', [SearchController::class, 'search'])->name('search');

Please visit the docs to learn more.

Source