How to make a custom model attribute column searchable and sortable?
Hey!
I have a getFullNameAttribute() method on my user model - how do i display that full name column in a resource, and then allow searching and sorting on it?
5 Replies
You can’t search or sort on model attributes as it’s not part of the eloquent query. You would need to have virtual db columns on the table to make that work.
I'm not familliar with virtual db columns...interesting!
Can’t find it at the moment, but iirc there’s something about them in the docs.
No worries, thanks I'll look into it! 🙂
Yeah you can do a virtual column it's pretty neat:
https://www.mydbops.com/blog/virtual-columns-mysql-use-cases
For Laravel specifically see
https://laraveldaily.com/post/virtual-db-columns-laravel-migrations-mysql
Virtual Columns in MySQL and Use cases.
Discover MySQL 5.7 virtual columns, which compute data using expressions. Learn their syntax, use cases, benefits, and indexing for optimized queries.
Laravel Daily
Virtual DB Columns in Laravel Migrations and MySQL