0

In Laravel view Undefined offset: 0

I got problem before says

expecting array in parameter 1 but object given

and fix it through ->toArray. And then appeared this problem

public function index()
    {
        $products = Product::paginate(3)->toArray();

        return view ('inventory.layout', [
            'products' => $products
        ]);
    }

And this is to show my products in view file I've tried to replace the 0 key with ['data'] string but useless.

  @if (isset($products)) 
        @if ($arrkeys = array_keys($products[0])) 
            @foreach ($arrkeys as $key)  
                <th>{{$key}}</th>
            @endforeach
        @endif 
   @endif
Roman Meyer
  • 2,634
  • 2
  • 20
  • 27
Mosalah
  • 81
  • 2
  • 9
  • 2
    in the controller, before you return the view, dump the contents of $products using `dd($products);` and see what is actually being returned by the function call. – Latheesan Aug 29 '19 at 12:10
  • I've already did this and showing me an three paginated array but when i want to display it in view, displaying this error "Undefined offset: 0" – Mosalah Aug 29 '19 at 12:22
  • Can you show us the response structure so we can compare it against the code you've written in the view and check what might be the issue – Latheesan Aug 29 '19 at 12:32
  • sure: array:12 [▼ "current_page" => 1 "data" => array:3 [▼ 0 => array:29 [▶] 1 => array:29 [▶] 2 => array:29 [▶] ] "first_page_url" => "http://localhost:8080/Import-CSV-Laravel/public/layout?page=1" "from" => 1 "last_page" => 10 "last_page_url" => "http://localhost:8080/Import-CSV-Laravel/public/layout?page=10" "next_page_url" => "http://localhost:8080/Import-CSV-Laravel/public/layout?page=2" "path" => "http://localhost:8080/Import-CSV-Laravel/public/layout" "per_page" => 3 "prev_page_url" => null "to" => 3 "total" => 30 ] – Mosalah Aug 29 '19 at 12:34
  • array:12 [▼ "current_page" => 1 "data" => array:3 [▼ 0 => array:29 [▶] 1 => array:29 [▶] 2 => array:29 [▶] ] – Mosalah Aug 29 '19 at 12:41
  • Possible duplicate of [Notice: Undefined offset: 0 in](https://stackoverflow.com/questions/6549561/notice-undefined-offset-0-in) – dparoli Aug 29 '19 at 13:03

2 Answers2

0

If you need to display pagination use code like this

public function index()
    {
        $products = Product::paginate(3);

        return view ('inventory.layout', [
            'products' => $products
        ]);
    }



And this is to show my products in view file
I've tried to replace the 0 key with ['data'] string but useless.



  @if (isset($products)) 
           @foreach ($products as $key)  
                <th>{{$key}}</th>
            @endforeach
    //for pagination
    <div>{{$products->links()}}</div>
  @endif
monu0002
  • 16
  • 1
0

If you want to display the keys of the array, try this:

  @if (isset($products))
      @foreach ($products as $key => $value)  
            <th>{{$key}}</th>
      @endforeach
  @endif

But If you want to display the values:

  @if (isset($products))
      @foreach ($products as $key => $value)  
            <th>{{$value}}</th>
      @endforeach
  @endif
Ahmad Karimi
  • 1,285
  • 2
  • 15
  • 26