yonka's avatar
Level 2

Laravel Search Related tables using Livewire

I have Five Tables named Items, Category, AttributeValues, InventoryItem and InventoryInStore. InventoryInStore Belongs to InventoryItem. InventoryItem belongs to Category and Item and BelongsTo Many AttributeValue Table. So when I fetch All data From InventoryInStore Table, I want to be able to search by Item name or Category Name and get Full Data.

Item Table

Id, name

Category Table

id name

Attribute Value

Id, Attribute_id, Value Name

InventoryItem name

Id, Item_id, Category_id

and belongs To Many Attribute Values attribute_value_inventory_item Table

attribute_value_id, inventory_item_id

InventoryInStore

Id, Inventory_item_id, Qty

That is how they are related.

What I want is to be able to get search inventoryInstore by name and category and get all item in inventory store with same item name and category.

And Also be able to select inside form and add quantity then save to database.

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve the desired functionality of searching the InventoryInStore table by Item name or Category name using Laravel and Livewire, you can follow these steps:

  1. Define Relationships in Models:

    First, ensure that the relationships between your models are properly defined. This will allow you to easily query related data.

  2. Create a Livewire Component:

    Create a Livewire component that will handle the search functionality.

    php artisan make:livewire InventorySearch
    
  3. Implement the Search Logic in the Livewire Component:

    In the InventorySearch component, implement the logic to search by Item name or Category name.

    // InventorySearch.php
    namespace App\Http\Livewire;
    
    use Livewire\Component;
    use App\Models\InventoryInStore;
    
    class InventorySearch extends Component
    {
        public $searchTerm;
    
        public function render()
        {
            $searchTerm = '%' . $this->searchTerm . '%';
    
            $inventoryInStores = InventoryInStore::whereHas('inventoryItem.item', function ($query) use ($searchTerm) {
                $query->where('name', 'like', $searchTerm);
            })
            ->orWhereHas('inventoryItem.category', function ($query) use ($searchTerm) {
                $query->where('name', 'like', $searchTerm);
            })
            ->with(['inventoryItem.item', 'inventoryItem.category'])
            ->get();
    
            return view('livewire.inventory-search', [
                'inventoryInStores' => $inventoryInStores,
            ]);
        }
    }
    
  4. Create the Blade View:

    Create a Blade view for the Livewire component to display the search results.

    <!-- resources/views/livewire/inventory-search.blade.php -->
    <div>
        <input type="text" wire:model="searchTerm" placeholder="Search by Item or Category Name">
    
        <table>
            <thead>
                <tr>
                    <th>Item Name</th>
                    <th>Category Name</th>
                    <th>Quantity</th>
                </tr>
            </thead>
            <tbody>
                @foreach($inventoryInStores as $inventoryInStore)
                    <tr>
                        <td>{{ $inventoryInStore->inventoryItem->item->name }}</td>
                        <td>{{ $inventoryInStore->inventoryItem->category->name }}</td>
                        <td>{{ $inventoryInStore->qty }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    
  5. Include the Livewire Component in Your View:

    Finally, include the Livewire component in your main view where you want the search functionality to appear.

    @livewire('inventory-search')
    

This setup will allow you to search the InventoryInStore table by Item name or Category name and display the results dynamically using Livewire. Make sure to adjust the relationships and field names according to your actual database schema if they differ.

Please or to participate in this conversation.