在laravel中搜索后如何获得“未找到”结果

时间:2019-11-25 07:23:28

标签: laravel laravel-blade

如果查询在数据库中没有匹配项,我希望得到“未找到”结果。

这是我的result.blade.php

@extends('layouts.app')

@section('content')
    @foreach ($result as $object)
        <div class="container pb-5">
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="card">
                        <div class="card-header">
                            <h3>Details for the animal</h3>
                        </div>
                        <div class="card-body">
                            <div class="col-12">
                                <p><strong>Serial Number: </strong>{{ $object->reference }}</p>
                                <p><strong>Animal Type: </strong>{{ $object->animal->type->category }}</p>
                                <p><strong>Farm: </strong>{{ $object->animal->user->name }}</p>
                                <p><strong>Date Of Birth: </strong>{{ $object->animal->dateOfBirth }}</p>
                                <p><strong>Farm Location: </strong>{{ $object->animal->user->address->city }}</p>
                                <p><strong>Clinic: </strong>{{ $object->animal->clinic->user->name ?? 'Was Not Checked by a Clinic' }}</p>
                                <p><strong>Clinic Location: </strong>{{ $object->animal->clinic->user->address->city }}</p>
                                <p><strong>Vaccination: </strong>
                                    {{ $object->animal->clinic->vaccine1 ?? 'N/A' }},
                                    {{ $object->animal->clinic->vaccine2 ?? 'N/A'}},
                                    {{ $object->animal->clinic->vaccine3 ?? 'N/A'}},
                                    {{ $object->animal->clinic->vaccine4 ?? 'N/A'}},
                                    {{ $object->animal->clinic->vaccine5 ?? 'N/A'}}</p>
                                <p><strong>Abattoir House: </strong>{{ $object->user->name }}</p>
                                <p><strong>Abattoir Location: </strong>{{ $object->user->address->city }}</p>
                                <p><strong>Slaughtered Date: </strong>{{ $object->created_at }}</p>
                                <p><strong>Slaughtered Weight: </strong>{{ $object->weight }} Kg</p>
                                <p><stong>Abattoir Displacement Date</stong>{{ $object->dateOfDisplacement }}</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="card">
                        <div class="card-header">
                            <h3><strong>About </strong>{{ $object->animal->user->name }}</h3>
                        </div>
                        <div class="card-body">
                            <div class="col-12">
                                <h4 style="font-style: italic; color: gray">{{ $object->animal->user->description ?? 'There is No Details about this farm'}}</h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    @endforeach

@endsection

当我遍历结果时,我不能使用“ if not”  或我的控制器。如您所见,我尝试了“ if not”(没有显示):

  public function getResult($serial_number) {
        $result = Slaughter::where('reference', 'like', "%{$serial_number}%")
            ->with('user', 'animal')
            ->latest()
            ->get();

        return view('search.result', compact('result'));

    }

欢迎您的帮助。

4 个答案:

答案 0 :(得分:7)

您可以使用forelse

@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse

阅读文档here

答案 1 :(得分:2)

您可以在视图中查看结果的大小。

@if(sizeof($result))
@foreach ($result as $object)
   //whatever you have to do
@endforeach
@else
  //else condition
@endif

或者您可以使用forelse循环

@forelse($result as $object)
   // result is found
@empty
  // result is not found
@endforelse

答案 2 :(得分:0)

您可以使用刀片if语句:https://laravel.com/docs/5.8/blade#custom-if-statements

所以您的代码可能是:

@extends('layouts.app')

@section('content')
    @if($result)
      @foreach ($result as $object)
         // content here
      @endforeach
    @else
        // Not found content here.
    @endif
@endsection

答案 3 :(得分:0)

@if (count($records) > 0)
    @foreach ($result as $object)
         //Your logic / printing
    @endforeach    
@else
    No record found
@endif