DolDurma
DolDurma

Reputation: 17340

Laravel access directly to json array in blade without using foreach statement

in this result:

LengthAwarePaginator {#251 ▼
  #total: 2
  #lastPage: 1
  #items: Collection {#246 ▼
    #items: array:2 [▼
      0 => ProductCategories {#247 ▼
        ...
        #attributes: array:6 [▼
          "id" => 1
          "category_name" => "test"
          "lang" => "fa"
          "images" => "{"images":{"original":"\/uploads\/post_images\/2017\/1512736029.jpeg","300":"\/uploads\/post_images\/2017\/300_1512736029.jpeg","600":"\/uploads\/post_images\/2017\/600_1512736029.jpeg","900":"\/uploads\/post_images\/2017\/900_1512736029.jpeg"},"thumbnail":"\/uploads\/post_images\/2017\/300_1512736029.jpeg"} ◀"
          "created_at" => "2017-12-08 10:47:56"
          "updated_at" => "2017-12-08 12:27:10"
        ]
        #original: array:6 [▼
          "id" => 1
          "category_name" => "test test"
          "lang" => "fa"
          "images" => "{"images":{"original":"\/uploads\/post_images\/2017\/1512736029.jpeg","300":"\/uploads\/post_images\/2017\/300_1512736029.jpeg","600":"\/uploads\/post_images\/2017\/600_1512736029.jpeg","900":"\/uploads\/post_images\/2017\/900_1512736029.jpeg"},"thumbnail":"\/uploads\/post_images\/2017\/300_1512736029.jpeg"} ◀"
          "created_at" => "2017-12-08 10:47:56"
          "updated_at" => "2017-12-08 12:27:10"
        ]
        ...
      }
      1 => ProductCategories {#248 ▶}
    ]
  }

which i get from this query:

$categories = ProductCategories::paginate(10);
dd($categories);

i'm trying to access to "images" column data such as thumbnail and 300 or etc, when i use foreach this data and show that on table i can't access to them, for example:

{{$categories->images->thumbnail}}

but i get Undefined property error and i can't show that

Upvotes: 0

Views: 938

Answers (3)

lagbox
lagbox

Reputation: 50531

How about you just cast that field to an array ...

class ProductCategories ...
{
    protected $casts = [
        'images' => 'array',
    ];

    ...
 }


 @foreach ($categories as $category)
     {{ $category->images['thumb'] }}
 @endforeach

Laravel 5.5 Docs - Eloquent - Mutators - Array and Json Casting

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163928

If you want to make it right, you should keep image related data in a separate table and use a relationship between category and image:

public function images()
{
    return $this->hasMany(Image::class);
}

But since you're keeping images data as JSON string, the first thing you want to do is to cast it to array. Add this to the model:

protected $casts = [
    'images' => 'array',
];

Then in Blade view:

@foreach ($categories as $category)
    {{ $category->images['thumbnail'] }}
@endforeach

Upvotes: 0

Laerte
Laerte

Reputation: 7083

The variable $categories is a Collection, so you need to get each individual object to get its data. For this you'll need to use a foreach.

@foreach($categories as $category)
    {{ $category->images }}
@endforeach

And to have access to thumbnail you'll need to decode the json string that you have in images. For that, you can use json_decode($category->images) function. This will return an array with thumbnail as key.

Upvotes: 0

Related Questions