Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to sort Laravel Eloquent through assignment function

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report