Get the App
SLTechnology News&Howtos  ›  Development  › 

How to sort Laravel Eloquent through assignment function

Shulou Source: shulou.com Published: 2022-06-01 04:35:27 10月03日 Update

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!

Tags: Sort function effect article result same success code method name done method more knowledge but industry information information channel channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi OPPO Reno MariaDB Redmi