Рюкзак- laravel Отношения в addcolumn - PullRequest
0 голосов
/ 08 апреля 2020

Я пытаюсь получить данные из связанных таблиц, но результата нет, у меня уже тысячи вариаций, нет надежды разобраться без посторонней помощи. У меня есть 2 таблицы:

Sensor
- id
- name
- etc

Датчик имеет в модели:

public function Sensor_log()

{
    return $this->hasMany(Sensor_log::Class, 'id_sensor');
}


Sensor_log
- id
- date_log
- id_sensor
- value

Sensor_log имеет в модели:

public function Sensor()
    {
        return $this->belongsTo(Sensor::class, 'id_sensor');
    }

В моем контроллере отображаются все доступные датчики у меня есть следующий код:

protected function setupListOperation()
{
    $sensor_types = config('app.global_sensor_types');
    $this->crud->addColumn('id');
    $this->crud->addColumn(['name' => 'name',        'label' => 'Sensor name',  'type' => 'text',]);
    $this->crud->addColumn(['name' => 'system_name', 'label' => 'System name',  'type' => 'text',]);
    $this->crud->addColumn(['name' => 'type_sensor', 'label' => 'Sensor type',  'type' => 'select_from_array','options' => $sensor_types]);
    $this->crud->addColumn(['name' => 'model_sensor','label' => 'Sensor model', 'type' => 'text',]);
    $this->crud->addColumn(['name' => 'gpio_num',    'label' => 'Port ID',      'type' => 'text',]);
    $this->crud->addColumn(['name' => 'is_active',   'label' => 'Is active',    'type' => 'boolean',]);
}

Когда я отображаю список датчиков, я также хочу получить последнее значение датчиков, которое находится в таблице Sensor_log

    $this->crud->addColumn(
        [
            // 1-n relationship
            'label'     => 'Select', // Table column heading
            'type'      => 'select',
            'name'      => 'id_sensor', // the column that contains the ID of that connected entity;
            'entity'    => 'sensor', // the method that defines the relationship in your Model
            'attribute' => 'value', // foreign key attribute that is shown to user
            'model'     => "sensor_log", // foreign key model
        ],
    );

Я также попробовал другой способ :

    $this->crud->addColumn(['name' => 'sensor_log.value',   'label' => 'last_value',    'type' => 'number',]);

Все мои попытки не увенчались успехом. Кто-нибудь, помогите мне, пожалуйста

...