Search code examples
phpmysqllaraveleloquentsingle-page-application

SpladeTable how to fetch a column from another table?


Started working on a project which will take care of the expiry dates of the articles.

Thought it would be nice to try out splade as a powerful tool for SPA's, as it's only neccessary to keep track of expiry dates (inserting them and listing the dates coming up)

I'm returning a Splade table with values, but since I'm a beginner I have no idea how to implement column from another table in this one.

This is the code for the table:

public function index()
{
    /*
    $items = Item::orderBy('expires_at')->get();
    
    return view('items', compact('items'));
    */
    return view('items', [
        'items' => SpladeTable::for(Item::class)
            ->column('id', 'id', true)
            ->column('barcode', 'Barcode', true)
            ->column('article_name', 'Article Name', true)
            ->column(Expiry::class, 'expires_at', true)
            ->searchInput('article_name', 'Type in article name')
            ->searchInput('barcode', 'Use the scanner')
            ->searchInput('sifra', 'Type in article number')
            ->defaultSort('expires_at')
            ->paginate(20),
    ]);
}

I have the column "expires_at" since the second parameter is for that, true is sortable, but have no idea how to fetch actual data.

Also, I updated the expires table so I have values in it related to items but they don't show.

Edit: forgot to mention everything works, checked DBeaver and all is good, table lists everything I want from the items table, just the expires_at column is empty.


Solution

  • I tried this and it didn't work the first time. Then I went onto discord and an user called J87 said to me:

    "Just use regular Laravel relations like ->column('relationName.column')"

    IT WORKS!