Search code examples
laravelquery-builderlaravel-5.7laravel-query-builder

laravel 5.7 whereNull


I want to get data from database where the value of specific column are null, i've searched from laravel doc query builder said use whereNull and i did but i got this error messages

htmlspecialchars() expects parameter 1 to be string, object given (View: /Applications/XAMPP/xamppfiles/htdocs/PROJECT/overtek/resources/views/pages/purchaseIndex.blade.php)

this are my controller

public function index()
{
  $pa = DB::table('account_pay_ables')
                ->whereNull('invoice')
                ->get();
  return view('pages.purchaseIndex',['pa'=>$pa]);
}

i've tried this one too same result

$pa = DB::select("SELECT count(*) as new FROM account_pay_ables  WHERE invoice IS NULL");

currently on my purchaseIndex.blade.php just this

@foreach($pa as $data)
  {{ $data}}
@endforeach

Solution

  • @foreach($pa as $data)
      {{ optional($data)->new }}
    @endforeach
    

    Optional function is a good wrapper for this, if an object is null, anything you access from it is null , else it outputs as normal.

    BUT, I suppose $data is an object, so you need to either transform it into a string, or put it into HTML, so do that first.