Table combine multiple values into one column with multiple rows
I know at a minimum I could do a custom view column to make this work. Just curious if I can do it without having to define a view. I want a table column that combines multiple database columns into one and splits it across multiple lines. For example I have the following Database columns:
* address_1
* address_2
* city
* state
* zip
* country
and I want to compose them to one table column showing a properly formatted address like:
Address 1
Address 2
City, State Zip
Country
I was trying to set address_1 as the table column and make the description using the other columns, but I could not figure out a way to make line breaks work in a description.
If I could just define line breaks in a description that'd be perfect. I'd probably end up using that in a few places to be honest.
Solution:Jump to solution
You could probably do it with state() or formatStateUsing() and return a string using HtmlString. But honestly, this is exactly what ViewColumns are for.
11 Replies
Solution
You could probably do it with state() or formatStateUsing() and return a string using HtmlString. But honestly, this is exactly what ViewColumns are for.
State I think is perfect. IMO I feel like a view column is a bit much for a simple string with linebreaks and makes more sense if you want to so something more fancy like combing an avatar and username into a column or something.
It's still good practice to separate your formatting from your logic, rather than embedding HTML markup in the logic.
Yeah I see your point.
That is a bit much for a closure, because like line 2 might be null so that break tag need to be conditional and everything
But thats good to know about the state method, Idk that I saw that in the docs
Yeah, I was going to say, I bet it's not as simple as just throwing some line breaks in, you'd want to check what fields are empty, etc.
Yeah for the most part its fine except the second line of the address. I did notice that sorting and filtering is broken because you can only sort/filter on the contents of an actual DB column, not sure if there is an easy fix or if I just need to not have sorting/filtering
Looks like searchable at least lets me pass a closure to customize the query
You could also create a custom getter inside your model.
And then you could access it "laravel-wide" by
MyModel->consolidatedAddress
which could result into filament like TextColumn::make('consolidatedAddress')
. @X7Ryan
Sorting and Filtering will be up to you by providing ->sortable()
closures.You can add ->searchable(['address1', 'address2', 'city']) or whatever you need, so the built in searching searches multiple columns for that text column.
But then you've got the same issue of embedding markup in logic, which is usually not a good idea. Computed attributes are fine for things like concating first_name and last_name, but not really for building HTML markup.
hm. kay - I agree.