0

Controller

$new_products = Product::with('images')->orderBy('created_at', 'desc')->take(10)->get();
return view('site/home', compact('new_products'));

View

{{ $new_product->images[0]->image_name }}

This gives me error undefined offset 0. How do i print the values of images?

values returned on dd($new_products) enter image description here

3 Answers 3

1

If you want to get the first item, you can use the first method.

{{ $new_product->images->first()->image_name }}

You also have the offsetGet method to get an item at a given offset.

{{ $new_product->images->offsetGet(0)->image_name }}

You can also loop through the collection though and do this:

@foreach ($new_product->images as $image)
    {{ $image->image_name }}
@endforeach

Note: The first two method will only work if your products have images. If they don't, then Laravel will return an empty collection. The third method of looping through the collection will work in all cases.

If you want to make sure that products have images, you can use the has method.

$new_products = Product::with('images')->has('images')->orderBy('created_at', 'desc')->take(10)->get();

This will only return products that have at least one image.

Docs on collection methods: http://laravel.com/docs/master/collections#method-first

Docs on relationships: http://laravel.com/docs/5.1/eloquent-relationships

Sign up to request clarification or add additional context in comments.

5 Comments

only third solution works. The first solution gives Trying to get property of non-object and the second one gives undefined offset 0
@AbelD Is it possible that some of your products don't have any images? The first two solutions will not work in that case. The third will work in all cases. The with method doesn't guarantee that the products have images. It can return an empty collection.
Thats right the second product dose not have images. Let me check.
ITs working. But how to tackle if images is not available for a product?
If you want to get only products that have images, then you should use the has method. $new_products = Product::with('images')->has('images')->orderBy('created_at', 'desc')->take(10)->get(); I edited the post with this information. :)
0

From the out it appears that $new_product is also an array of two items,

{{ $new_product[0]->images[0]->image_name }}

Edit: Thomas Kim has a better answer

1 Comment

its from @foreach($new_products as $new_product)
0

Your relation (images) is a collection object, if you all() method on that then you'll get the underlying array so then you can access any item from array using the index:

{{ $new_product->images->all()[0]->image_name }}

Also toArray() method will work:

{{ $new_product->images->toArray()[0]['image_name'] }}

So, either pass the array to your view like $new_product->all() or loop it. You may check the documentation here for more information about Collection object in Laravel.

1 Comment

Oops! My mistake, I forgot to change the object noation to array notation while using toArray() but all() should work as it is in the example, anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.