How do I sort the list alphabetically in the Standard?

Asked

Viewed 2,495 times

3

I have a query that lists me several users I want to sort these users by alphabetical order as I can do ?

php

private function getChilds(array $elements, $parentId = 0) {

    $branch = array();

    foreach ($elements as $element) {
        if ($element['parent'] == $parentId) {
            $children = $this->getChilds($elements, $element['id']);
            if ($children) {
                foreach ($children as $child){
                    $branch[] = $child;
                }
            }
            $branch[] = $element;
        }
    }

    return $branch;
}

public function lista_jogadores (){

    $user_id = Auth::user()->id;

    $players = DB::table('players')->where('activo', '=', '1')->where('agent', '=', $user_id)->get();

    $childs = DB::table('agents')->select('id', 'username', 'parent')->orderBy('username', 'ASC')->get();
    $childs_arr = array();
    foreach ($childs as $child){
        $child_arr = array(
            'id' => $child->id,
            'username' => $child->username,
            'parent' => $child->parent
        );
        $childs_arr[] = $child_arr;
    }
    $parents = $this->getChilds($childs_arr, $user_id);

    return view('admin.templates.jogadores', ['players' => $players])->with('parents',$parents);

} 
  • Don’t understand what you want to do? Sort what? Type if you have an example of the table and how should the data come? Because you have a getChilds event?

2 answers

1

$childs = DB::table('agents')
       ->select('id', 'username', 'parent')
       ->where('id', '>=', $user_id)
       ->orderBy('CAMPO', 'asc')
       ->get();
  • I put it like this but not this to order does not effect

0

According to the documentation, use the method orderBy.

$childs = DB::table('agents')
->select('id', 'username', 'parent')
->where('id', '>=', $user_id)
->orderBy('username', 'ASC')
->get();
  • I have a user called admin ordered so appear last should not appear first being in alphabetical order the A and first that the letter T for example is what I do not understand

  • You are ordering by username really? Try to change the order by decreasing and see how it behaves: ->orderBy('username', 'DESC')

  • I’ve tried not to change anything there’s a code other than this one

  • I’ve already put the code all together as low or as low as nothing changes

  • What is the result of var_dump( $childs ); ?

  • the result and this you can see here https://pastebin.com/UdLFUbFF

  • Beauty. And what is the result of var_dump($parents)?

  • sorry for the delay but I had this one https://pastebin.com/0rsdYJHf

  • Your array is not returning in alphabetical order. Try to reorder it before passing it to the view.

  • How can I order alphabetically before I go to the view and what I don’t know

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.