0

I am trying to fetch data from blade template. I am trying but it does not show me anything. In database table there is a value 10 against u_id. when my code is like

@foreach($school_data as $val)
  <?php 
      $datas2 = \App\criteria::where('u_id', $val->u_id)->get();
      $trck2= $datas2['0']->updated_by_email;
   ?>
@endforeach

it shows Undefined offset: 0 . But when it is a static value

@foreach($school_data as $val)       
 <?php 
      $datas2 = \App\criteria::where('u_id', '10')->get();
      $trck2= $datas2['0']->updated_by_email;
   ?>
@endforeach

then it works properly. How can I fix it?

Thanks in advance

2 Answers2

0

Array index have is an integer, use :

$trck2= $datas2[0]->updated_by_email;
  • Try this if you want to get single row : ` $datas2 = \App\criteria::where('u_id', $val->u_id)->first(); var_dump($datas2);` – anggriyulio Feb 10 '19 at 18:07
0

I suggest to print the values of $val->u_id into the @foreach to debug your code.

Then the proper way to assign a value to a php variable in blade is using the @php directive.
How to Set Variables in a Laravel Blade Template
https://laravel.com/docs/5.7/blade#php

@foreach($school_data as $val)
    {{$val->u_id}}
    @php ($datas2 = \App\criteria::where('u_id', $val->u_id)->get()) @endphp
    @php (dump($datas2) ) @endphp
    @php ($trck2 = $datas2['0']->updated_by_email) @endphp
@endforeach
Davide Casiraghi
  • 15,591
  • 9
  • 34
  • 56