簡體   English   中英

僅在使用PHP在Laravel中通過搜索找到查詢時顯示

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

我有推薦代碼搜索框,用於查詢推薦代碼。 用戶可以注冊之前,此代碼必須可用。 因此,基本上在Auth.register上,我有兩個框,第一個框包含引薦代碼搜索框,另一個是注冊框。 我的目標是先隱藏注冊框,僅在找到代碼后才顯示。

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

我現在的問題是,@details顯示“引用”的當前行

有沒有辦法從這一行進行查詢,

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

僅在結果與$ keyword相匹配時才顯示?

因為當前頁面看起來像這樣

在此處輸入圖片說明 謝謝!

我認為您希望將關鍵字與數據庫進行匹配。 因此,使用部分匹配而不是部分匹配。

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

並且鑒於您不需要使用以下幾行。

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

只需在@if(isset($details))之后添加您的注冊表格@if(isset($details))

您如何做到這一點很奇怪。 您可以使用AJAX在單個頁面中完成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM