Attempt to read property "id" on bool when trying to export pdf

106 Views Asked by At

i want to export data to pdf based on certain id, when i clicked on the button, it returning an Attempt to read property "id" on bool, i've tried debugging the $data variable in the controller, and it's returning the records on certain id

PDFController.php

<?php

namespace App\Http\Controllers;

use App\Models\Contract;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;
use App\Models\User;
use Filament\Infolists\Infolist;

class PDFController extends Controller
{
    public function downloadpdf($id)
    {
        $contract = Contract::with(['surveys', 'surveys.surveyors', 'surveys.assets'])
            ->where('id', $id)
            ->first();

        $users = User::all();

        $data = [
            'date' => $contract->selesai_kontrak,
            'users' => $users,
            'contract' => $contract
        ];

        dd($data);

        $pdf = PDF::loadView('contractPDF', $data);

        return $pdf->download('ContractDetails.pdf');
    }
}

contractPDF.blade.php

<!DOCTYPE html>
<html>

<head>
    <title>Contract Details</title>
</head>

<body>
    <h1>Contract Details</h1>
    <p>Date: {{ $date }}</p>

    <div>
        <h2>Contract ID: {{ $contract->id }}</h2>
        <p>Pemberi Tugas: {{ $contract->pemberi_tugas }}</p>
        <p>Jenis Industri: {{ $contract->industries->type }}</p>
        <p>Tujuan Kontrak: {{ $contract->contract_types->type }}</p>
        <p>Lokasi Proyek: {{ $contract->lokasi_proyek }}</p>

        @foreach ($contract->surveys as $survey)
            <h3>Survey ID: {{ $survey->id }}</h3>
            <p>Surveyor: {{ $survey->surveyors->name }}</p>
            <p>Pemilik Aset: {{ $survey->pemilik_aset }}</p>
            <p>Tanggal Survey: {{ $survey->tanggal_survey }}</p>
            <p>Jenis Aset: {{ $survey->assets->type }}</p>
            <p>Keterangan Aset: {{ $survey->keterangan_aset }}</p>
            <img src="{{ asset('storage/' . $survey->gambar_aset) }}" alt="Gambar Aset">
            <p>Harga Aset: Rp. {{ number_format($survey->harga_aset, 0, '.', '.') }}</p>
        @endforeach
    </div>
</body>

</html>

Contract.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Contract extends Model
{
    use HasFactory;

    protected $fillable = [
        'surveyors_id',
        'surveys_id',
        'pemberi_tugas',
        'industries_id',
        'contract_types_id',
        'lokasi_proyek',
        'tanggal_kontrak',
        'selesai_kontrak',
        'status_kontrak',
        'durasi_kontrak',
        'is_available',

    ];

    public function surveys(): HasOne
    {
        return $this->hasOne(Survey::class);
    }

    public function surveyors(): BelongsTo
    {
        return $this->belongsTo(Surveyor::class);
    }

    public function contract_types(): BelongsTo
    {
        return $this->belongsTo(ContractType::class);
    }

    public function industries(): BelongsTo
    {
        return $this->belongsTo(Industry::class);
    }

    public function setStatusKontrakAttribute($value)
    {
        $this->attributes['status_kontrak'] = $value;

        if ($value === 'In Progress') {
            $this->attributes['is_available'] = 1;
        } elseif ($value === 'Selesai' || $value === 'Batal') {
            $this->attributes['is_available'] = 0;
        }
    }
}

what should i do to my code? is there any changes needed?

0

There are 0 best solutions below