yonka's avatar

Read All TakenInventoryInfo and Paginate it

I want to read TakenInventoryInfo and Paginate all by also reading StoreFrom from TakenInventories. This is my database design 1.TakenInventoryInfo

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

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

    public function takenInventories()
    {
        return $this->hasMany(TakenInventory::class);
    }

    public function store()
    {
        return $this->belongsTo(Store::class);
    } // store to which is taken From.
}

2.TakenInventory

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

    protected $fillable = ['taken_inventory_info_id','inventory_in_store_id','quantity','store_id'];

    public function inventoryInStore()
    {
        return $this->belongsTo(InventoryInStore::class);
    }

    public function store()
    {
        return $this->belongsTo(Store::class);
    } // store taken to
    public function takenInventoryInfo()
    {
        return $this->belongsTo(TakenInventoryInfo::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

}
  1. User
class User extends Authenticatable
{
    use HasFactory, Notifiable, HasRoles;

    // user belongs to branch
    public function branch(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Branch::class);
    }

    public function stores()
    {
        return $this->belongsToMany(Store::class);
    }
}

Each User BelongsToMany Store. I want to be able to read (by checking stores to which system user belongsTo ) all these attributes: tracking_no, Store_from(TakenInventoryInfo), Store_to(TakenInventories), taken_date. and has a link to view all TakenInventories related to this TakenInventoryInfo and be able to Print or download as pdf using barryvdh/laravel-dompdf. I am using Livewire and Laravel. I also want to be able to search by tracking_no or taken_date or store_to. Also I want to add pagination to search items

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! Below is a step-by-step, production-ready solution for your requirements using Eloquent, Livewire, and with support for search and pagination.


1. Eloquent Query for Paginated, Searchable Data

Create a Livewire component (e.g. TakenInventoryInfoIndex). Here’s how you can load, filter, and paginate the data:

Livewire Component: TakenInventoryInfoIndex.php


2. Your Livewire Blade View


3. Controller for PDF Export

public function print($id)
{
    $info = TakenInventoryInfo::with(['store', 'takenInventories.store'])->findOrFail($id);
    $pdf = \PDF::loadView('pdf.taken-inventory-info', compact('info'));
    return $pdf->download("taken-inventory-info-$id.pdf");
}

4. Example of PDF Blade View (resources/views/pdf/taken-inventory-info.blade.php)

<h1>Taken Inventory Info: {{ $info->tracking_no }}</h1>
<p>Store From: {{ $info->store->name ?? '-' }}</p>
<p>Taken Date: {{ $info->taken_date }}</p>

<h2>Taken Inventories</h2>
<ul>
@foreach($info->takenInventories as $taken)
    <li>
        Store To: {{ $taken->store->name ?? '-' }},
        Quantity: {{ $taken->quantity }}
    </li>
@endforeach
</ul>

5. Routes Example (routes/web.php)

Route::get('taken-inventory-info/{id}/show', ...)->name('taken-inventory.info.show');
Route::get('taken-inventory-info/{id}/print', [YourController::class, 'print'])->name('taken-inventory.info.print');

6. Notes

  • This setup is searchable, filterable by store_to, and paginated.
  • The PDF generation uses barryvdh/laravel-dompdf.
  • Adjust as needed for authorization, policies, or more complex filtering.
  • For view all TakenInventories of TakenInventoryInfo, link to a detail/show page.

Let me know if you need further details or a sample controller for the CRUD/show endpoint!

Please or to participate in this conversation.