0

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

4
  • Do you need the pagination? Commented Dec 8, 2017 at 12:51
  • @Chamara Abeysekara no. i dont have any problem for paginate data, my problem is access directly to json data into images such as thumbnail Commented Dec 8, 2017 at 12:52
  • You will get Undefined property error if any one link is not present in your database. For eg- catagory_id 1,2,4 is present in related table but category_id 3 is not present in related table. Commented Dec 8, 2017 at 12:59
  • Can you do something like ProductCategories::get()->pluck('images') and {{json_encode($categories)}} ?? Commented Dec 8, 2017 at 13:00

3 Answers 3

1

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

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

5 Comments

by default i'm using $casts in Model. my problem is access to json array without using foreach into datatables to show only thumbnail column
its a COLLECTION, how do you think you are going to get a single 'thumb' nail from an 'image' inside the collection (which are many) without asking for a single element from the collection or iterating it ... $categories contains many ... $categories->images .. what would that even imply .. which 'category' do you want the images from ... are you getting it yet?
without using foreach i can access to thumbnail by this code {{$categories[0]->images['thumbnail']}}
"without asking for a single element from the collection or iterating it" ... you can't (as your comment is getting a single element).. that was the point .. .its all the same you have to access an element of the collection first .. then you can get to that information .. its all the same
could you help me on this topic ? stackoverflow.com/q/47717191/1830228 Thanks in advance
0

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.

2 Comments

yes i know thats collection, in table as data pagination i cant use foreach and show add data of collection, i want to access to only thumbnail not all data
Since it's a JSON string, you will probably have to decode each one of it. I don't know if there is a direct way to get it.
0

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

1 Comment

could you help me on this topic ? stackoverflow.com/q/47717191/1830228 Thanks in advance

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.