A
A
Alexander2015-09-23 15:18:05
Laravel
Alexander, 2015-09-23 15:18:05

Trees in Laravel, how to create an array?

The global task is to display a tree-like list of files.
There is a table with files file_contracts (id, contract_id, name, parent_id);
There is a table with contracts (id, name, date, executor)
Set up a connection in the contract model:

// список файлов к договору
    public function files()
    {
        return $this->hasMany('App\FileContract');
    }

In the controller, I get the contracts element
public function show($id)
    {
        $contract=Contract::find($id);
// тут мне надо превратить $contract->files в массив, которые описан ниже
$files = array();

         return view('contract.view')->with('contract',$contract)->with('files',$files);

I need to get an array like:
$documents[0]=array(
            'name'  => 'File 1',
            'id'    => '2',
            'child' => array(
                    '1' => array (
                        'name' => 'sub File 1',
                        'id' => '5',
                        'child' => array (
                                '1' => array (
                                    'name' => 'sub sub file 1',
                                    'id' => '12',
                                ),
                                '2' => array (
                                    'name' => 'sub sub file 2',
                                    'id' => '1342',
                                )
                        )
                    ),
                '2' => array (
                    'name' => 'sub file 2',
                    'id' => '5',
                    'child' => array (
                        '1' => array (
                            'name' => 'sub sub file 1',
                            'id' => '12',
                        ),
                        '2' => array (
                            'name' => 'sub sub file 2',
                            'id' => '1342',
                        )
                    )
                )
            )
        );

I can’t understand how in the Laravel structure I can make a function with recursion in the controller action to get this array, or do I need to do it somewhere else?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-09-24
@Al3xanderG

As a result, for the time being I solved the problem as follows, added a new method in the Contract model, to which I feed the array obtained using the table connection:

public function makeContractFiles($array,$level) {
        if(count($array)>0) {
            foreach($array as $key => $value) {
                // выполняем перебор массива и ищем все элементы уровня level
                if($value['parent_id']==$level) {
                    // начинаем собирать новый массив
                    $files[]=array(
                        'name'  => $value['title'],
                        'id'    => $value['id'],
                        'child' => $this->makeContractFiles($array,$value['id']),
                    );
                }
            }
        }

        if(isset($files)) {
            return $files;
        } else {
            return 0;
        }
    }

This method gives me an array, which is already quietly processed in the blade template and produces either a tree with UL lists or a SELECT selection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question