How to sort Laravel Eloquent through assignment function
Editor to share with you how Laravel Eloquent is sorted by assignment functions. I hope you will get something after reading this article. Let's discuss it together.
Sort by assignment function
Imagine that you have code like this:
Function getFullNameAttribute () {return $this- > attributes ['first_name']. ''. $this- > attributes ['last_name'];}
Now, do you want to sort by "full_name"? It was found to be ineffective:
$clients = Client::orderBy ('full_name')-> get (); / / has no effect
The solution is simple. We need to sort the results after we get them.
$clients = Client::get ()-> sortBy ('full_name'); / / success!
Notice that the method name is different-- it's not orderBy, it's sortBy.
After reading this article, I believe you have a certain understanding of "how Laravel Eloquent is sorted by assignment function". If you want to know more about it, please follow the industry information channel. Thank you for your reading!