-
Hi everyone 👋 My question is about the Column::make functionality and how it interfaces with the Model to retrieve data when we are not targeting a specific attribute from the table, rather an attribute that is retrieved through an accessor method. According to this discussion this should work, however I'm having an issue with the missing column error. (SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.full_name' ...) In my example I have a users table in the db that has first_name and last_name attributes. accessor method:
In the the UserTable the column looks like this:
I have a feeling this should work, and I don't understand why it isn't working so should this work, and do you have any advice on how would I go about understanding on why it doesn't? Update: I found this discussion as well noting that accessor methods worked in v1, but don't work in v2 so clear cut answer would be helpful to decide if this is supported, and if not maybe to open up a ticket for it to be re-introduced. If anyone is looking for a workaround querying on the model works with the format() or label() option on the column. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
So! The accessor methods won't work as those all feed into the Builder query, and so they need to be actual database fields. A few different options here. I've got a PR open #1082 - which will allow you to add some selectRaw columns in there, so you'll be able to do a concat easily enough and return it as a field for use. Not sure if/when that'll make it into core though! Another alternative of course depending on your database software is to define the attribute in there Or you can of course as you said create a Label column. |
Beta Was this translation helpful? Give feedback.
-
As of now I'll use a solution inspired by imabug and I will consider this to be by design for the v2 version, which as you stated requires actual database fields. In the meantime if anyone is looking for a clean solution I suggest using a This would be my recommended way of getting to the accessor methods in v2:
|
Beta Was this translation helpful? Give feedback.
-
@Edim7 - another quick point, if you use the Builder function then you can retrieve the relevant attributes, and you won't need to do the additional model load, not to mention that you'll end up doing it 100 times if you've got 100 rows to a page, which will quickly slow things down. If you create a new discussion/issue with your component, then I'll give you a few pointers on how to do it efficiently where I can. Don't forget that with format() you have the following: ->format(
fn($value, $row, Column $column) => $row->first_name . ' ' . $row->last_name
), If you use with() in your builder method, then you can access the attribute via $row->attributeName, and it removes the necessity to do your lookup. |
Beta Was this translation helpful? Give feedback.
So! The accessor methods won't work as those all feed into the Builder query, and so they need to be actual database fields.
A few different options here.
I've got a PR open #1082 - which will allow you to add some selectRaw columns in there, so you'll be able to do a concat easily enough and return it as a field for use. Not sure if/when that'll make it into core though!
Another alternative of course depending on your database software is to define the attribute in there
Or you can of course as you said create a Label column.