簡體   English   中英

如何檢查 laravel 中是否存在數據庫行?

[英]How do i check if database row exist in laravel?

您好,我如何檢查 laravel 中是否存在數據庫行插入? 如果> 0,我想顯示一些條目,如果條目 = 0,我想顯示其他內容。但我不知道該怎么做。 我嘗試使用 forelse,否則我會遇到同樣的錯誤。 未定義變量 $istProj。

<div class="card-body">

@if ($istoric->isEmpty())
    @forelse ($istoric as $istProj)
        <div class="mb-3">
            <table class='table'>
                <tr class="table-row-heads">
                    <th>Id Proiect</th>
                    <th>Tip actiune </th>
                    <th>Colaborator </th>
                    <th>Suma </th>
                    <th>Data </th>
                </tr>
                <tr class="table-row-data">
                    <td>{{ $istProj->id_proiect }}</td>
                    <td>{{ $istProj->action_type }}</td>
                    <td>{{ $istProj->colaborator_id }}</td>
                    <td>{{ $istProj->suma }}</td>
                    <td>{{ $istProj->data }}</td>
                </tr>
            </table>
        </div>
    @empty
        <div class="card-body">
        <h1>Nu au fost gasite inregistrari</h1>
        </div>
    @endforelse
    @endif

</div>
<form action="{{ url('/') }}" method="POST">
    @csrf
    @method('PUT')
    <div class="mb-3">
        <label class="form-label">Id proiect</label>
        <input type="text" class='form-control' value="{{ $proiecte->id }}" name='id_proiect' id='id_proiect' placeholder="Id proiect">
    </div>

    <div class="mb-3">
        <label class="form-label">Tip actiune</label>
        <select class="form-select" aria-label="Default select example"  name='Status_Tranzactii'>
            <option selected>Alege tipul actiunii (0 = cheltuiala, 1 = plata, 2 = incasare)</option>
            <option value="cheltuiala">0</option>
            <option value="plata">1</option>
            <option value="incasare">2</option>
        </select>
    </div>

    <div class="mb-3">
        <label class="form-label">Colaborator</label>
        <select class="form-select" aria-label="Default select example" name="Colab_id">
            <option selected>Alege colaboratorul (daca este cazul)</option>
            @foreach ($colaborator as $colaboratori)
            <option value="{{ $colaboratori->id }}">{{ $colaboratori->id }} </option>
            @endforeach
        </select>

    </div>

    <div class="mb-3">
        <label class="form-label">Suma</label>
        <input type="text" class='form-control' value="{{ $istProj->suma }}" name='suma' placeholder="Introduceti suma">
    </div>

    <div class="mb-3">
        <label class="form-label">Data</label>
        <input type="text" class='form-control' value="{{ $istProj->data }}" name='data' placeholder="Introduceti data">
    </div>
    <button type='submit' class='btn btn-primary' style="float: right;">Adauga</button>
</form>

我怎樣才能讓它發揮作用?

“Suma”在線錯誤

要檢查 Laravel 中的數據庫表中是否存在一行,可以使用查詢構建器的 exists 方法。

以下是如何使用 exists 方法檢查表中是否存在行的示例:

use Illuminate\Support\Facades\DB;

$exists = DB::table('users') ->where('email', 'john@example.com') ->exists();

如果我理解得很好,您已經有了一個收集結果(來自 Eloquent 或任何地方),您需要根據該結果執行不同的操作。

我是正確的? 如是....

要檢查數量或條目,您可以使用$result->count() ,因此在 Blade 中您可以:

@if($result->count() > 0)
    Do something
@else
    Do anotherthing
@endif

暫無
暫無

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

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