Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

COUPDEGRACES's avatar

DOMPDF With selected Date

I have blade like this

 <thead>
                            <tr>
                                <th width="2%">#</th>
                                <th width="20%">Koran</th>
                                <th>Info Iklan</th>
                                <th width="20%">Diterima</th>
                                <th width="15%">Harga</th>
                            </tr>
                        </thead>
                        <tbody>
                        @php
                        $no=1;
                        @endphp
                        @foreach($orders as $order)
                        <tr>
                            <td>{{ $no++ }}</td>
                            <td>{{ $order->subdivisi }}</td>
                            <td>{{ $order->title }}</td>
                            <td>{{ $order->waktupesan }}</td>
                            <td>{{ $order->harga }}</td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                    <h5>Total: {{ $sum }}</h5>

And controller like this

    public function checkReport(Request $request){
        if ($request->date){
            $date = $request->date;
            $orders = DB::table('iklan')->where('waktupesan',$date)->get();
            $sum = DB::table('iklan')->where('waktupesan',$date)->sum('harga');
             return view('laporan.result',compact('orders','sum','date'));
           // dd($request->date);
        }

When i input date in my datepicker, its worked , the problem is when im download pdf using dompdf package , all the data that I printed suddenly all my data out, it doesn't match the datepicker I use.

my AdminController.php

    public function pdf1(Request $request)
    {
        $iklan = Iklan::with('user:id,name,created_at')->orderBy('created_at', 'asc')->take(50)->get();
        $pdf1 = \PDF::loadView('pdf1', compact('iklan'))->setPaper('A4', 'landscape');
        return $pdf1->stream();
    }
0 likes
15 replies
a4ashraf's avatar

@letmeknow

you are not selecting the date in the pdf1 function


public function pdf1(Request $request)
    {
            $date = $request->date;

        $iklan = Iklan::with('user:id,name,created_at')->orderBy('created_at', 'asc')->where('waktupesan',$date)->take(50)->get();
        $pdf1 = \PDF::loadView('pdf1', compact('iklan'))->setPaper('A4', 'landscape');
        return $pdf1->stream();
    }
a4ashraf's avatar

@letmeknow

btw you can refactor you checkRepot function

  public function checkReport(Request $request){
        if ($request->date){
            $date = $request->date;
            $orders = DB::table('iklan')->where('waktupesan',$date)->get();
            $sum = $orders->sum('harga');
             return view('laporan.result',compact('orders','sum','date'));
           // dd($request->date);
        }
COUPDEGRACES's avatar

im trying

    public function pdf1(Request $request)
    {
        $date = $request->date;
        $iklan = Iklan::with('user:id,name,created_at')->orderBy('created_at', 'asc')->where('waktupesan', $date)->take(50)->get();
        $pdf1 = \PDF::loadView('pdf1', compact('iklan'))->setPaper('A4', 'landscape');
        return $pdf1->stream();
    }

when im searching 2021-03-26 https://prnt.sc/10rvh49

but when im print

https://prnt.sc/10rvhgk no data out sir

COUPDEGRACES's avatar

@a4ashraf in Iklan.php (model)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Iklan extends Model
{
    protected $table = 'iklan';

    protected $fillable = [
        'id_user', 'id-halaman', 'subdivisi', 'title' , 'harga', 'waktupesan', 'status'
    ];

    public function user()
    {
        return $this->belongsTo('App\User', 'id_user');
    }
}

a4ashraf's avatar

@letmeknow

check this output and wouldn't use ->get().

make sure waktupesan is date filed in your migration

$iklan = Iklan::with('user:id,name,created_at')
				->where('waktupesan', $date)
				->orderBy('created_at', 'asc')
				->take(50);
COUPDEGRACES's avatar

when im tried to dd variable iklan i got resdult like this

Illuminate\Database\Eloquent\Builder {#1125 ▼
  #query: Illuminate\Database\Query\Builder {#401 ▼
    +connection: Illuminate\Database\MySqlConnection {#211 ▶}
    +grammar: Illuminate\Database\Query\Grammars\MySqlGrammar {#229 ▶}
    +processor: Illuminate\Database\Query\Processors\MySqlProcessor {#230}
    +bindings: array:9 [▼
      "select" => []
      "from" => []
      "join" => []
      "where" => []
      "groupBy" => []
      "having" => []
      "order" => []
      "union" => []
      "unionOrder" => []
    ]
    +aggregate: null
    +columns: null
    +distinct: false
    +from: "iklan"
    +joins: null
    +wheres: array:1 [▼
      0 => array:3 [▼
        "type" => "Null"
        "column" => "waktupesan"
        "boolean" => "and"
      ]
    ]
    +groups: null
    +havings: null
    +orders: array:1 [▼
      0 => array:2 [▼
        "column" => "created_at"
        "direction" => "asc"
      ]
    ]
    +limit: 50
    +offset: null
    +unions: null
    +unionLimit: null
    +unionOffset: null
    +unionOrders: null
    +lock: null
    +operators: array:30 [▶]
    +useWritePdo: false
  }
  #model: App\Iklan {#392 ▼
    #table: "iklan"
    #fillable: array:7 [▼
      0 => "id_user"
      1 => "id-halaman"
      2 => "subdivisi"
      3 => "title"
      4 => "harga"
      5 => "waktupesan"
      6 => "status"
    ]
    #connection: null
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: false
    +wasRecentlyCreated: false
    #attributes: []
    #original: []
    #changes: []
    #casts: []
    #classCastCache: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #guarded: array:1 [▼
      0 => "*"
    ]
  }
  #eagerLoad: array:1 [▼
    "user" => Closure($query) {#1192 ▼
      class: "Illuminate\Database\Eloquent\Builder"
      use: {▶}
      file: "C:\xampp\htdocs\absensi-app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php"
      line: "1170 to 1180"
    }
  ]
  #localMacros: []
  #onDelete: null
  #passthru: array:19 [▼
    0 => "insert"
    1 => "insertOrIgnore"
    2 => "insertGetId"
    3 => "insertUsing"
    4 => "getBindings"
    5 => "toSql"
    6 => "dump"
    7 => "dd"
    8 => "exists"
    9 => "doesntExist"
    10 => "count"
    11 => "min"
    12 => "max"
    13 => "avg"
    14 => "average"
    15 => "sum"
    16 => "getConnection"
    17 => "raw"
    18 => "getGrammar"
  ]
  #scopes: []
  #removedScopes: []
}
viorel's avatar

I think you made a typo near Iklan::with(user:id... (there is a column which shouldn't be there) Try one of the following code snippets below.

public function pdf1(Request $request)
    {
        $date = $request->date;
        $iklan = Iklan::with('user_id,name,created_at')->orderBy('created_at', 'asc')->where('waktupesan', $date)->take(50)->get();
        $pdf1 = \PDF::loadView('pdf1', compact('iklan'))->setPaper('A4', 'landscape');
        return $pdf1->stream();
    }

Or

public function pdf1(Request $request)
    {
        $date = $request->date;
        $iklan = Iklan::with('user,id,name,created_at')->orderBy('created_at', 'asc')->where('waktupesan', $date)->take(50)->get();
        $pdf1 = \PDF::loadView('pdf1', compact('iklan'))->setPaper('A4', 'landscape');
        return $pdf1->stream();
    }

Or, based on the fillable fields from your model,

public function pdf1(Request $request)
    {
        $date = $request->date;
        $iklan = Iklan::with('id_user,name,created_at')->orderBy('created_at', 'asc')->where('waktupesan', $date)->take(50)->get();
        $pdf1 = \PDF::loadView('pdf1', compact('iklan'))->setPaper('A4', 'landscape');
        return $pdf1->stream();
    }

Hope this helps you.

COUPDEGRACES's avatar

@viorel thats user:id get from model Iklan.php

    public function user()
    {
        return $this->belongsTo('App\User', 'id_user');
    }

when im try to DD , no data in #items

Illuminate\Database\Eloquent\Collection {#1127 ▼
  #items: []
}
viorel's avatar

@letmeknow I can see that you try to use Eager Loading here. In that case try to use this code

public function pdf1(Request $request)
    {
        $date = $request->date;
        $iklan = Iklan::with('user')->orderBy('created_at', 'asc')->where('waktupesan', $date)->take(50)->get();
        $pdf1 = \PDF::loadView('pdf1', compact('iklan'))->setPaper('A4', 'landscape');
        return $pdf1->stream();
    }

For eager loading to work you must pass the name of the relationship (user in your case). Try this with a dd()

Please or to participate in this conversation.