1

In a database table - about_us, I have a column named subject

I am trying to make a table from this data in laravel

Column subject looks like this:

[
    {
        "name1": "1",
        "name2": "2"
    },
    {
        "name1": "3",
        "name2": "4"
    }
]

from controller

public function about()
{
    $about  = About::find(1);
    return view('pages.about-us', compact('about'));
}

in about-us.blade.php

{{$about->subject}}

How can I extract the data to make a table like this? enter image description here

1
  • You could start writing some code for that - what have you tried, where are you stuck? Without any details about the logic, there is no way to help you Commented Nov 13, 2019 at 10:55

4 Answers 4

2

The contents of your subject property is a JSON string. Which means that you will have to decode it through json_decode() into an array/object before your can iterate over it.

<table>
    <thead>
        <tr>
            <th>right</th>
            <th>left</th>
        </tr>
    </thead>
    <tbody>
        @foreach(json_decode($about->subject) as $subject)
            <tr>
                <td>{{ $subject->name1 }}</td>
                <td>{{ $subject->name2 }}</td>
            </tr>
        @endforeach
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

Comments

1

Try code below:

<table>
    <tr>
        <td>right</td>
        <td>left</td>
    </tr>
    @foreach(json_decode($about->subject) as $subject)
        <tr>
            <td>{{ $subject->name1 }}</td>
            <td>{{ $subject->name2 }}</td>
        </tr>
    @endforeach
</table>

Comments

0
<table>
<thead>
    <th><strong>right</strong></th>
    <th><strong>left</strong></th>
</thead>
<tbody>
@foreach($about->subject as $subject)
        <tr>
            <td>{{ $subject->name1 }}</td>
            <td>{{ $subject->name2 }}</td>
        </tr>
    @endforeach
</tbody>
</table>

Just loop over the data

Comments

0

Try this

<table>
<thead>
      <tr>
        <th>Right</th>

        <th>Left</th>

       </tr>
</thead>
<tbody>
@foreach($about->subject as $subject)
    <tr>
        <td>{{ $subject->name1 }}</td>
        <td>{{ $subject->name2 }}</td>
    </tr>
@endforeach


</tbody>
</table>

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.