A
A
antonio02262018-04-27 18:51:27
Laravel
antonio0226, 2018-04-27 18:51:27

How to correctly display data in the template?

I've been killing Laravel for 3 days now.
At the moment, the following response is stored in the database:

[["c"],["b"],["a"],["a"],["c"],["b"],["b"],["b"],["c"],["a"],["b"],["b"],["c"],["c"],["c"],["c"],["b"],["b"],["b"],["a"],["c"],["c"],["c"],["c"],["a"],["c"],["c"],["a"],["a"],["a"],["b"],["a"],["b"],["b"],["b"],["a"],["a"],["a"],["b"],["b"],["b"],["a"],["c"],["b"],["a"],["b"],["b"],["a"],["c"],["b"],["b"],["b"],["a"],["a"],["b"],["c"],["a"],["c"],["a"],["a"],["b"],["b"],["c"],["a"],["a"],["c"],["c"],["a"],["a"],["b"],["c"],["b"],["a"],["b"],["c"],["b"],["b"]]

I put it there like this:
Answer::create([
                    'name' => $request->name,

                    'answer' => json_encode($answers), // Вот он

                    'a' => $answer_a,
                    'b' => $answer_b,
                    'c' => $answer_c,
                ]);

How can I now transfer it to the view so that you can access each cell?
Tried like this
$name = Answer::select('name')->get();
        $answers = json_decode(Answer::select('answer')->get()[0], true);
        return view('home')->with(['name' => $name, 'answers' => $answers]);

In html, I tried like this . As a result, I got each character, because first a bracket, then a bracket, a quote, etc. How can I implement all this? This is the kind of table. The data will be replenished and you need to display info in the admin panel .....
<li>{{ $answers[3] }}</li>
5ae346e850514234969898.png

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
Y
Yan-s, 2018-04-27
@Yan-s

https://laravel.ru/docs/v5/eloquent-mutators#%D0%B...
It is not required to perform json_encode and json_decode.
in this line
you get the model instance, not the answer field. Firstly, you need to get rid of json_decode, secondly, you will have the required field in $answers-
>
answer
use

return view('home', ['name' => $name, 'answers' => $answers]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question