Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yonka's avatar

The easiest way to read data from related tables using Livewire

I have these tables 1.BroughtInventoryInfo

class BroughtInventoryInfo extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['tracking_no','brought_date','store_id','reference','description','approve_status','user_id','status'];

    protected $table = 'brought_inventory_infos';

    public function broughtInventories()
    {
        return $this->hasMany(BroughtInventory::class);
    }

    public function store() // store_from
    {
        return $this->belongsTo(Store::class);
    }
}

2.BroughtInventory

class BroughtInventory extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['store_id','inventory_item_id', 'quantity'];

    public function inventoryItem()
    {
        return $this->belongsTo(InventoryItem::class);
    }
    public function store()
    {
        return $this->belongsTo(Store::class); // store_to
    }
    public function broughtInventoryInfo()
    {
        return $this->belongsTo(BroughtInventoryInfo::class);
    }
}

I want to be able to read from these tables: -by choosing store_from, store_to, tracking_no, brought_date, status: The user can choose store_from ( in the table: brought_inventories_info) and be able to choose other fields like store_to ( in the table: brought_inventories), brought_date, tracking_no, status. the store_to field is enabled when the user chooses store_from. Using Livewire and laravel

1 like
2 replies
LaryAI's avatar
Level 58

To achieve this with Livewire, you’ll want to build a component that reacts to the user’s selections, loads related data, and exposes it to your view. Here’s a step-by-step example of how to set this up.


1. Livewire Component Setup

Generate a Livewire component (if you haven’t yet):

php artisan make:livewire BroughtInventoryBrowser

2. Component Class Example


3. Livewire Blade View Example


How it works:

  • The user selects “Store From” (store_from) and Livewire loads related “Store To” options.
  • User can further filter by tracking_no, brought_date, or status.
  • Upon submitting, it queries with the related relationships and displays the results.

Tips:

  • Adjust relationships and column names if your actual schema varies.
  • Use eager loading judiciously to avoid N+1 queries.

Let me know if you need a more specific implementation or extra refinements!

vincent15000's avatar

The easiest way to read data from related tables using Livewire ?

Hmmm ... why do you think that with Livewire you have to do that another way ?

With or without Livewire, if you want to access data from related tables, you need to define relationships.

Please or to participate in this conversation.