Reputation: 520
I am trying to fetch data from database, but I am getting an error Undefined Offset 8
. It seems like the data that I am trying to fetch is not available in database.
But as array_key_exists()
function with objects is deprecated in php 7.4, I am not sure how to check if an offset/ array key exists
Here is my code :-
Blade
//Other codes
@for($i=0;$i<13; $i++)
{{$modules[$i]}}
@endfor
Controller
public function index(){
$data['modules'] = DB::select('users')->where('user_deleted_flag','!=',true)->get();
return view('index', $data);
}
Web.php
Route::get('index', 'ViewController@index');
So, how do I check if offset value upto 13 or data exists?
Upvotes: 0
Views: 378
Reputation: 174
you can use foreach in blade:
@foreach($modules as $key => $module)
{
{{ $module[$key] }}
}
@endforeach
Upvotes: 1
Reputation: 807
You could take the minimum of the # of elements in the array and 13.
@for($i = 0; $i < min(count($modules), 13); $i++)
{{ $modules[$i] }}
@endfor
Alternatively you could limit the resultset of the query on the backend to 13 if that would be something you'de like. DB: Query Builder Skip/Take
Upvotes: 1
Reputation: 340
You can use if in blade
@if(count($modules))
@for($i=0;$i<13; $i++)
{{$modules[$i]}}
@endfor
@endif
Upvotes: 1
Reputation: 534
You can use isset
@for($i=0;$i<13; $i++)
@if(!isset($modules[$i]))
@break
@endif
{{$modules[$i]}}
@endfor
Upvotes: 1