0

I have the following data model(equipment rental):

order(id,client_id,status), equipment(id, title, rental_price, status, category_id). equipment_unit(id, equipment_id, code) order_items(order_id, equipment_unit_id, rented_at, due_at, rental_price)

Relation: Order

public function firstItem()
{
    return $this->hasOne(OrderItem::class)->oldestOfMany();
}

public function getFirstItemAttribute()
{
    return $this->firstItem()->first();
}


public function client()
{
    return $this->belongsTo(Client::class, 'client_id');
}

public function items()
{
    return $this->hasMany(OrderItem::class);
}

//Relation OrderItem
public function order()
{
    return $this->belongsTo(Order::class);
}

public function equipmentUnit()
{
    return $this->belongsTo(EquipmentUnit::class);
}

public function equipmentModel()
{
    return $this->hasOneThrough(
        EquipmentModel::class,
        EquipmentUnit::class,
        'id',                // Foreign key on equipment_units
        'id',                // Foreign key on equipment_models
        'equipment_unit_id', // Local key on order_items
        'model_id' // Local key on equipment_units
    );
}

//Relation EquimentUnit
public function model()
{
    return $this->belongsTo(EquipmentModel::class, 'equipment_model_id');
}

public function equipment()
{
    return $this->belongsTo(EquipmentModel::class, 'model_id');
}

public function orders()
{
    return $this->hasMany(Order::class, 'equipment_unit_id');
}

I need to output information to a table with order_items.Field rented_at and due_at.I'm outputting but unfortunately there are duplicates, I just need to take first item.

public function columns(): array
{
    return [
        Column::make('ID', 'id')->sortable()->searchable(),
        DateColumn::make('Rental date', 'firstItem.rented_at')
            ->inputFormat('Y-m-d H:i:s') 
            ->outputFormat('d.m.Y')
            ->sortable(),
        Column::make('Client', 'client.name')->searchable(),
        Column::make('Phone', 'client.phone')->searchable(),
        Column::make('Equipment','equipment')
            ->label(function ($row) {
                return $row->items
                    ->map(function ($item) {
                        return $item->equipmentUnit->equipment->title
                            . ' (' . $item->equipmentUnit->code . ')';
                    })
                    ->implode('<br>');
            })
            ->html(),

        Column::make('Action')
            ->label(fn($row) => view('livewire.orders.table-actions', ['id' => $row->id,
                'status' => $row->status]))->html(),

    ];
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.