вот пример функции, которая получает только значения из модели:
Код: Выделить всё
$where = [];
$where['state'] = PirepState::ACCEPTED;
$where['airline_id'] = $airline_id;
$where['source'] = PirepSource::ACARS;
$average_lrate = Pirep::where($where)->avg('landing_rate');
$stats[__('DBasic::widgets.alrate')] = number_format(abs($average_lrate)) . ' ft/min';
Код: Выделить всё
$where = [];
$where['state'] = PirepState::ACCEPTED;
$where['airline_id'] = $airline_id;
$where['source'] = PirepSource::ACARS;
$average_grate = Pirep::where($where)->avg('fields->where(slug, "landing-g-force")->value(value)->all()');
$stats[__('DBasic::widgets.agrate')] = number_format(abs($average_grate)) . ' g';
Код: Выделить всё
/**
* Get the pirep_fields and then the pirep_field_values and
* merge them together. If a field value doesn't exist then add in a fake one
*/
public function fields(): Attribute
{
return Attribute::make(get: function ($_, $attrs) {
$custom_fields = PirepField::whereIn('pirep_source', [$this->source, PirepFieldSource::BOTH])->get();
$field_values = PirepFieldValue::where('pirep_id', $this->id)->orderBy(
'created_at',
'asc'
)->get();
// Merge the field values into $fields
foreach ($custom_fields as $field) {
$has_value = $field_values->firstWhere('slug', $field->slug);
if (!$has_value) {
$field_values->push(
new PirepFieldValue([
'pirep_id' => $this->id,
'name' => $field->name,
'slug' => $field->slug,
'value' => '',
'source' => PirepFieldSource::MANUAL,
])
);
}
}
return $field_values;
});
}
Код: Выделить всё
/**
* Return a custom field value
*
* @param $field_name
*
* @return string
*/
public function field($field_name): string
{
$field = $this->fields->where('name', $field_name)->first();
if ($field) {
return $field['value'];
}
return '';
}
Код: Выделить всё
$average_grate = Pirep::where($where)->avg('fields->where(slug, "landing-g-force")->value(value)->all()');
Столбец не найден: 1054 Неизвестные поля столбца в «списке полей»
Код: Выделить всё
$average_grate = Pirep::where($where)->avg('fields.value->where(slug, "landing-g-force")->get()');
Столбец не найден: 1054 Неизвестный столбец «fields.value» в «списке полей»
Подробнее здесь: https://stackoverflow.com/questions/790 ... ches-value
Мобильная версия