How to fix the problem of displaying message when the product does not exist

Viewed 38

Currently I am having problem with notification when data is not in database

My Controller

    public function index(Request $request)
    {
        // Get the search value from the request
        $search = $request->input('search');
        $products = null;
        if($search != null) {
            // Search in the title and body columns from the posts table
            $products = product::query()
                ->where('mavach', 'LIKE', "%{$search}%")
                ->get();
        }
        if (empty($products)) {
            return view('index', compact('products'))->withErrors(['no_post_result' => 'Serial không đúng hoặc không tồn tại trên hệ thống']);
        }
        else {
            return view('index', compact('products'));
        }
    }

My blade

<div class="col-md-8">
            <div class="form h-100">
                <h3><b>Kiểm tra bảo hành</b><img src="#" class="float-right" width="10%" height=""></h3>
                <form class="mb-5" name="baohanh" action="{{ route('products.search') }}" method="GET" id="demoForm">
                    @csrf
                    <div class="row">
                        <div class="col-md-12 form-group mb-5">
                            <input class="form-control" type="text" name="search" id="search-auto" cols="30" rows="4" value="" class="validate">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12 form-group" style="pading-top: 20px">
                            <input type="submit" name="mavach" value="Tra cứu" class="btn btn-primary rounded-0 py-2 px-4"> <a href="https://anhngoc.vn" class="float-right btn btn-primary" target="blank">
                                <span  style="margin-top: 25px !important;">Trang chủ</span></a>
                        </div>
                    </div>
                    @if(isset($_REQUEST['mavach']))
                    @foreach ($products as $product)
                <div class="row">
                    <div class="col-md-12">
                        <p class="h6"><b>Thông tin chi tiết sản phẩm:</b></p>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-sm">
                        <tbody>
                        <tr>
                            <td class="col-sm-3 h6">Số serial number: </td>
                            <td colspan="2" class="col-sm-9 h6">{{$product->mavach}}</td>

                        </tr>
                        <tr>
                            <td class="col-sm-3 h6"> Ngày chứng từ: </td>
                            <td colspan="2" class="col-sm-9 h6">{{$product->ngayct}}

                            </td>
                        </tr>
                        <tr>
                            <td class="col-sm-3 h6">Tên sản phẩm: </td>
                            <td colspan="2" class="col-sm-9 h6">{{$product->tenvt}}</td>
                        </tr>
                        <tr>
                            <td class="col-sm-3 h6">Thời hạn bảo hành: </td>
                            <td colspan="2" class="col-sm-9 h6">{{$product->tgbh}}</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                @endforeach
                        @error('no_post_result')
                            <div class="alert alert-danger">{{ $message }}</div>
                        @enderror
                @endif
            </div>
        </div>

When submitting if the product is in the database it displays normally, but if it is not, it doesn't show the message. I'm a beginner learning PHP and Laravel, I've searched a lot on the web, but the problem is still not fixed.

1 Answers

Add one more condition in if of the blade to check if $product is not empty

@if(isset($_REQUEST['mavach']) && !empty($products))
Related