Laravel Nova Log

Ornek olarak projenizde yer alan fiyat teklifi modelinin urunler modeli ile iliskili bir yapisi var. Fiyat teklifi olustururken eklediginiz urunlerin adet bilgilerini pivot tabloda tutuyorsunuz. eklenen urunlerin fiyatlarinin pivot tablodaki adet bilgisi ile carpilarak butun urunlerin sonuc degerleri toplayip teklif modelinizde goruntulemek istiyorsunuz. Buraya kadar yaptiginiz islemlerin sonuclarini laravel log dosyasinda butun detaylari ile saglikli calisip calismadigini kontrol etmek icin izlemek istiyorsunuz. Bunun icin oncelikli olarak teklif modelindeki funksiyonlarimiza, \Log::info tanimi eklemeliyiz.

// app/Model/Quote.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Quote extends Model
{
    use HasFactory;

    protected $fillable = [
        'reference', 'client_id', 'object', 'notes', 'status', 'total_ht', 'total_ttc',
    ];

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

    public function articles() {
        return $this->belongsToMany(Article::class, 'quote_article')->withPivot('quantity');
    }

    public function getTotalHtAttribute() {
        $total_ht = 0;
        foreach ($this->articles as $article) {
            \Log::info("Article: " . json_encode($article));
            $quantity = $article->pivot->quantity;
            $price = $article->selling_price;
            $total_ht += $quantity * $price;
            \Log::info("Quantity: {$quantity}, Price: {$price}, Total HT: {$total_ht}");
        }
        return $total_ht;
    }
    
    public function getTotalTtcAttribute() {
        $total_ttc = 0;
        foreach ($this->articles as $article) {
            \Log::info("Article: " . json_encode($article));
            $quantity = $article->pivot->quantity;
            $price_ttc = $article->price_including_tax;
            $total_ttc += $quantity * $price_ttc;
            \Log::info("Quantity: {$quantity}, Price TTC: {$price_ttc}, Total TTC: {$total_ttc}");
        }
        return $total_ttc;
    }
}
// app/Nova/Quote.php

public function fields(NovaRequest $request)
    {
        return [
            ID::make(__('ID'), 'id')
                ->sortable()
                ->size('w-1/2'),
            Date::make('Date de création', 'created_at')
                ->size('w-1/2')
                ->sortable()
                ->hideWhenCreating()
                ->hideWhenUpdating(),
            BelongsTo::make('Client', 'client', Client::class)
                ->size('w-1/3')
                ->searchable(),
            Text::make('Objet', 'object')
                ->size('w-1/3')
                ->hideFromIndex()
                ->nullable(),
            Select::make('Statut', 'status')
                ->size('w-1/3')
                ->sortable()
                ->options([
                    'Crée' => 'Crée',
                    'Facturé' => 'Facturé',
                    'En retard' => 'En retard',
                    'Annulé' => 'Annulé',
                ])
                ->default('Crée'),
            Textarea::make('Notes', 'notes')
                ->alwaysShow()
                ->nullable(),
            Number::make('Total HT', 'total_ht')
                ->resolveUsing(function () {
                    return $this->resource->total_ht;
                })
                ->size('w-1/3')
                ->min(0)
                ->step(0.01)
                ->sortable(),
            Number::make('Total TTC', 'total_ttc')
                ->resolveUsing(function () {
                    return $this->resource->total_ttc;
                })
                ->size('w-1/3')
                ->min(0)
                ->step(0.01)
                ->sortable(),
            Text::make('Référence', 'reference')
                ->size('w-1/3')
                ->sortable()
                ->default(Str::random(8)),
            BelongsToMany::make('Articles', 'articles', Article::class)
                ->searchable()
                ->fields(function () {
                    return [
                        Number::make('Quantité', 'quantity')
                        ->min(1)
                        ->default(1)
                        ->step(0.01)
                        ->sortable(),
                    ];
                }),
        ];
    }