Home > Software engineering >  How do i check if database row exist in laravel?
How do i check if database row exist in laravel?

Time:12-31

Hello how do i check if database row insert exist in laravel? i want to display something entries if > 0 and i want to display else if entries = 0. but i don t know how to do that. i tried with forelse, if else and i got same error. Undefined variable $istProj.

<div >
                            @if ($istoric->isEmpty())
                            @forelse ($istoric as $istProj)
                                <div >
                                    <table class='table'>
                                        <tr >
                                            <th>Id Proiect</th>
                                            <th>Tip actiune </th>
                                            <th>Colaborator </th>
                                            <th>Suma </th>
                                            <th>Data </th>
                                        </tr>
                                        <tr >
                                            <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 >
                                <h1>Nu au fost gasite inregistrari</h1>
                                </div>
                            @endforelse
                            @endif

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

                            <div >
                                <label >Tip actiune</label>
                                <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 >
                                <label >Colaborator</label>
                                <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 >
                                <label >Suma</label>
                                <input type="text" class='form-control' value="{{ $istProj->suma }}" name='suma' placeholder="Introduceti suma">
                            </div>

                            <div >
                                <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>

How can i make that to work?

Error on line with "Suma"

CodePudding user response:

To check if a row exists in a database table in Laravel, you can use the exists method of the query builder.

Here is an example of how you can use the exists method to check if a row exists in a table:

use Illuminate\Support\Facades\DB;

$exists = DB::table('users') ->where('email', '[email protected]') ->exists();

CodePudding user response:

If I understand well, you already have a Collection result (From Eloquent or anywhere) and you need to do different action based on that result.

I'm,right? If yes....

To check the amount or entries you can use $result->count(), so in Blade you can:

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