Laravel: Correct way to create a Search in a Form

  laravel, search

In the database there are 1000 User and 2000 Items for sign out. There is a Transaction Form where you select User then the Item they are borrowing then click Submit.

I’m trying to create a Search for User and Item. I’m avoiding third-party Laravel plugins like Select2.

Is there a better way to search through the Users and Items?

User Model

    public function transactions()
    {
        return $this->hasMany(Transaction::class);
    }

Item Model

    public function transactions()
    {
        return $this->hasMany(Transaction::class);
    }

Transaction Model

    public function item()
    {
        return $this->belongsTo(Item::class);
    }

    public function patron()
    {
        return $this->belongsTo(Patron::class);
    }

Code in my drop-down list:


                <div class="form-group">
                    <label for="patron">Patron</label>
                    <select name="patron" id="patron" class="form-control">
                        @foreach($patrons as $patron)
                            <option value="{{ $patron->id }}"
                                @if(isset($transaction))
                                    @if($patron->id == $transaction->patron_id)
                                    selected
                                    @endif
                                @endif
                            >
                                {{ $patron->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="form-group">
                    <label for="item">Item</label>
                    <select name="item" id="item" class="form-control">
                        @foreach($items as $item)
                            <option value="{{ $item->id }}"
                                   @if(isset($transaction))
                                        @if($item->id == $transaction->item_id)
                                            selected
                                        @endif
                                    @endif
                            >
                                {{ $item->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

Source: Laravel

Leave a Reply