简体   繁体   中英

Display only if query is found via search in Laravel using php

I have referral code search box that query referral codes. this code must be available before user can register. So basically on Auth.register I have two boxes, firs t box holds the referral code search box and the other one is the registration box. My goal is to hide first the registration box and appear only if the code has found.

RegisterController

public function index(Request $request)
{
    $keyword = $request->get('search');
    $referral = Referral::where('code', 'LIKE', '%'.$keyword.'%')->get();

    if (count ( $referral ) > 0)
        return view ( 'Auth.register')->withDetails ( $referral )->withQuery ( $keyword );
    else
        return view ( 'Auth.register')->withMessage ( 'The code you provided is not existing.' );
}

register.blade.php

<!-- THIS IS THE FIRST BOX THAT HOLDS THE CODE SEARCH BOX -->
<div class="card mx-4">
    <div class="card-body p-4">        
       <h1>Referral Code</h1>
       <p class="text-muted">Please provide the referral code given to you by your collector</p>
        @if(isset($message))
            <div class="alert alert-success">
                {{ $message }}
            </div>
        @endif 

        {!! Form::open(['method' => 'GET', 'url' => '/register', 'class' => 'form-inline my-2 my-lg-0', 'role' => 'search'])  !!}
        <div style="width: 100%;">
            <input type="text" class="form-control" name="search" style="width: 100%;" placeholder="Enter your referral code here!">
        </div>
        <div style="width: 150px; margin: auto; margin-top: 20px; ">
            <button class="btn btn-success btn-default" type="submit">
                <i class="fa fa-search"></i> Check Availability
            </button>
        </div>
        {!! Form::close() !!}
    </div>
</div> 

<!-- THIS IS THE 2ND BOX THAT HOLDS THE REGISTRATION FORM -->
@if(isset($details))   
<div class="card mx-4">
    <div class="card-body p-4">
        @foreach($details as $code)
           @if($code->status==0)
             <h2>Code is available</h2>
           @else
             <h2>Code is already taken</h2>
            @endif
        @endforeach

     ... the rest of registration form ...
    </div>
</div>
@endif

My problem now is, the @details is displaying the current rows of "Referrals"

Is there a way that query from this line,

$referral = Referral::where('code', 'LIKE', '%'.$keyword.'%')->get();

should only display if the result is match from $keyword?

because currently the page is look like this

在此处输入图片说明 Thanks!

I think you want something like to match the keyword against your database. So instead of partial matching use exact matching.

public function index(Request $request)
{
    $keyword = $request->get('search');
    $referral = Referral::where([
                         ['code', $keyword],
                         ['status', 0]
                         ])->first();

    if ($referral)
        return view ( 'Auth.register')->withDetails ( $referral )->withQuery ( $keyword );
    else
        return view ( 'Auth.register')->withMessage ( 'The code you provided does not exist or already used.' );
}

And in view you need not to use the following lines.

@foreach($details as $code)
    @if($code->status==0)
        <h2>Code is available</h2>
    @else
        <h2>Code is already taken</h2>
    @endif
@endforeach 

Just add your registration form after @if(isset($details)) .

Its weird how you are doing this. You can use AJAX to do it in a single page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM