Search code examples
phpmysqllaravelxampplaravel-8

Search user from their Id# after search button is pressed Laravel 8


This is my output screenI want to show registered user data after his/her id# is entered in the input field and on hit enter or when search button is pressed bring his/her data in the fields. I just want to show user data when the search button is pressed. I think the problem is in my controller. This is my controller:

use Illuminate\Http\Request;
use App\Helpers\Helper;
use Illuminate\Support\Facades\DB;
use App\User;
use App\Models\patient;

class Helpercontroller extends Controller
{
function search(Request $request){
     $patientid = $request->input('patientid');
    


    $data = DB::table('patients')->where(['patientid'=>$patientid])->first();

    return $data;
}
    


    
}
    
        
    }

this is my route file:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\login;
use App\Http\Controllers\MainController;
use App\Http\Controllers\add1;
use Illuminate\Http\Request;
use App\Http\Controllers\Helpercontroller;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::get('/registration', function () {
    return view('registration');
});

Route::post('/registration/search',[Helpercontroller::class,'search'])->name("search-registration");

this is my blade file:


@extends('layouts.theme')
@section('content')

<div class="container">
    <div class="row">
        <div class="col-lg-12">

        <div class="col-md-3 col-md-offset-3 d4 " style="margin-top: 50px">

    <h4>Patient Registration</h4>
</div><br>

        @if(Session::get('success'))
<div class="alert alert-success">
    {{Session::get('success')}}
</div>
@endif  

@if(Session::get('fail'))
<div class="alert alert-danger">
{{Session::get('fail')}}
</div>
@endif

<form action="{{ route("search-registration") }}" method="POST" autocomplete="off">
            @csrf
                <label>Search Patient :</label>
            <input placeholder="Enter Patient ID" type="number" name="patientid" class="form-control form-control-sm d2"  >
                </div>

                </div>
                        <div class="col-lg-2">
                        <button type="submit" class="btn-block col-lg-12 ">Search</button>
                        </div>
</form>
[![THIS IS MY BLADE FILE OUTPUT][1]][1]



Solution

  • change your route for search, a good practice is name the routes for better organization:

    Route::post('/registration/search',[Helpercontroller::class,'search'])->name("search-registration");
    

    In the form:

    <form action="{{ route("search-registration") }}" method="POST" autocomplete="off">