0

I am having trouble displaying data from my MySQL json-type column.

Controller

public function index()
    {
        $layaways = Layaway::orderBy('created_at', 'desc')->get();

        $layaways = paginate($layaways);

        return view('admin/layaway.index')->with('layaways', $layaways);
    }

View

<tbody>
    @foreach ($layaways as $layaway)
         <tr data-toggle="collapse" data-target="#id{{ $layaway->transaction_id }}">
              <td>{{ $layaway->transaction_id }}</td>
         </tr>

         <td colspan="4" id="id{{ $layaway->transaction_id }}" class="collapse p-0" data-parent="#accordion">
              <div class="row ml-0 mr-0">
                   <div class="card col-md-6 pr-0 pl-0">
                       <div class="card-header text-center">
                          Layaway Products
                       </div>
                       <div class="card-body m-5">
                          @foreach ($layaway->selected_products as $product)
                             {{ $product->id }}
                          @endforeach
                       </div>
                   </div>
              </div>
          </td>
      @endforeach
</tbody>

Sample data from dd()

sample data from dd

Sample data from dd() after Tharaka Dilhsan suggested

enter image description here

Error received

Invalid argument supplied for foreach()

Question

  • How can I properly display the json data which is inside the attribute field?

4 Answers 4

1

You can use 'Casting' in 'Eloquent Model'

Layway Model

class Layway extends Model
{
    protected $casts = [
        'selected_peoducts' => 'array'
    ];
}

this will automattically cast your 'selecred_products' json data to an array.

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

3 Comments

I tried the answer you've provided but unfortunately, it returns an error htmlspecialchars() expects parameter 1 to be string, array given... I also tried to insert a new record after applying changes that you have recommend based on your answer, and it changes the format of the data. It appears that it has a slash on each of the beginning.
as the screenshot of dd() you have provided in the question, 'selected_products' is a string, not an array. can you provide the dd() output with this approach please.
I added an image on my post above. Sorry for late feedback. Thank you.
0

You can try this:

return view('admin/layaway.index')->with('layaways', $layaways->toArray());

Comments

0

your inner array considers as a string so please convert the inner string to array and then try hope it's working.

Comments

0

I got it working after a trial and error with tremendous research.

I just wrapped the $layaway->selected_products using json_decode(). Thank you.

From this

<div class="card-body m-5">
    @foreach ($layaway->selected_products as $product)
       {{ $product->id }}
    @endforeach
</div>

To this

<div class="card-body m-5">
    @foreach (json_decode($layaway->selected_products) as $product)
       {{ $product->id }}
    @endforeach
</div>

Comments

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.