0

I am displaying my data using datatable, if empty array then datatable will not be shown.

I tried

@if(count($results) > 0)
    //data show
@foreach($results as $r)
    <p>{{$r->name}}</p>
@endfor
@else
    No result found.
@endif
    //
    // $results is an array

This will return error if empty $result. How can i make it if $result is empty then show the custom message and not to display DataTable.

Crazy
  • 847
  • 1
  • 18
  • 41

5 Answers5

0

You should try this:

@if(isset($results) && !empty($results))

    <?php
      print('<pre style="color:red;">');
    print_r($results);
    print('</pre>');


    ?>

  @foreach($results as $r)
   <p>{{$r->name}}</p>
  @endforeach
@else
  No result found.
@endif
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
0

Use isEmpty() to check object empty or not-

@if (!$result->isEmpty())

Sohel0415
  • 9,523
  • 21
  • 30
0

We do have a shorthand in blade for the foreach/empty scenarios.

@forelse ($results as $r)
    <p>{{ $r->name }}</p>
@empty
    No result found
@endforelse

From the code you have and the comment you have of how you get $results, $results should be an array of stdClass objects, which means that error should not be happening. Since the code you have is limited, I can't be sure the error is coming from that code at all.

My guess is that $results isn't an array but is an object. count(new stdClass) === 1 count(new App\User) === 1, etc ...

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • $results = DB::select($query); $query = "select * from table..." i pass the $results to blade. – Crazy Nov 27 '17 at 06:36
0

Maybe is safer to check at the controller level and return 0 if $result is NULL

After you get the collection just check this:

$result = $result ? $result : 0

And return $result.

Radu
  • 995
  • 12
  • 23
0

You are ending the foreach loop with endfor. It should be ended with endforeach.

If this do not fix your error then please update your question with your controller code. Is your results variable a collection object?

Ashish
  • 468
  • 2
  • 8
  • 24
  • Just mistype. The controller just $query = "select * from products" $results = DB::select($query); and then pass to blade only. – Crazy Nov 27 '17 at 07:01
  • You can not directly pass the query in the select. Either you use DB::raw or try this $results = DB::table('products')->all(); – Ashish Nov 27 '17 at 07:08
  • i passed $results...not the $query to blade – Crazy Nov 27 '17 at 07:10
  • Yes, I understand that. Please look at this. https://laravel.com/docs/5.4/queries#raw-expressions You can not pass complete query directly in select statement instead you need to use DB::raw... else use this query $results = DB::table('products')->get(); and pass $results in blade – Ashish Nov 27 '17 at 07:14
  • Ikr. But i don't think this will caused the if-else statement to work? It will also facing the problem i mentioned. In my case, I able to display everything fine while there's data. I just want to catch when there's no record then run into else statement to show no result found. – Crazy Nov 27 '17 at 07:19
  • What do you get if you just write {{$results}} in blade? – Ashish Nov 27 '17 at 07:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159873/discussion-between-ashish-and-zidance). – Ashish Nov 27 '17 at 07:37