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/[email protected]/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/[email protected]/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!
You can only access theroute()
method on named routes. In yourweb.php
file, you must give your search route the namesearch
before you can access it viaroute('search')
In youweb.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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.