1

Let's say I have this Laravel Eloquent model:

$scores = [];
$dataScores = Book::where('author_id', '=', $uid)
            ->select('score')
            ->get();

Which results in:

[{
    "score": 30
},
{
    "score": 100
},
{
    "score": 17
},
{
    "score": 75
},
{
    "score": 17
},
{
    "score": 0
},
{
    "score": 60
},
{
    "score": 100
},
{
    "score": 17
},
{
    "score": 67
},
{
    "score": 83
},
{
    "score": 50
},
{
    "score": 100
},
{
    "score": 50
},
{
    "score": 83
},
{
    "score": 38
},
{
    "score": 90
},
{
    "score": 10
},
{
    "score": 83
},
{
    "score": 83
},
{
    "score": 60
},
{
    "score": 80
},
{
    "score": 13
},
{
    "score": 33
},
{
    "score": 33
}]

So, I figure before I return the view(), I would do this:

    for ($s=0; $s < count($dataScores); $s++) {
        array_push($scores, $dataScores[$s]->score);
    }

    return view(
        'layouts.dashboard.main',
        [
            'menu' => 'book-dashboard',
            'scores' => $scores
        ]
    );

So, I would just access {{ $scores }} in the Laravel Blade view and I thought I would get

$scores =  [
    30,
    100,
    17,
    75,
    17,
    0,
    60,
    100,
    17,
    67,
    83,
    50,
    100,
    50,
    83,
    38,
    90,
    10,
    83,
    83,
    60,
    80,
    13,
    33,
    33
];

But instead I get:

htmlspecialchars() expects parameter 1 to be string, array given.

But If I include the before-modified array of just the key/value of score / x.. then it returns fine. I just want the score values in an array.

3 Answers 3

2

First of all, you may omit this code:

for ($s=0; $s < count($dataScores); $s++) {
        array_push($scores, $dataScores[$s]->score);
    }

Instead, just use:

$dataScores = Book::where('author_id', '=', $uid)
            ->select('score')
            ->get()
            ->pluck('score);

This will result in an array like $scores = [1,2,3,4,5...]

Obviously you can not render it with blade like {{$scrores}} You have to implode the array at first.

Do it this way:

return view(
        'layouts.dashboard.main',
        [
            'menu' => 'book-dashboard',
            'scores' => implode(" ", $scores);
        ]
    );

You will get an output of "1 2 3 4 5 6..."

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

2 Comments

Actually he can render that variable into blade because the collection object has implemented the __toString method
Oh, whoa! I must have missed that pluck in the documentation! Thank you. I get this.
0

Try like this:

$scores = [];
$dataScores = Book::where('author_id', '=', $uid)
            ->select('score')
            ->get()
            ->toArray();

        foreach($dataScores as $key => $value){
            array_push($scores,$value);
        }    

        return view(
        'layouts.dashboard.main',
        [
            'menu' => 'book-dashboard',
            'scores' => $scores
        ]
    );

Comments

0

I think you can use pluck collection method to avoid manually do loop and get a mapped array with only the key/value that you need

<?php
$scores = Book::where('author_id', '=', $uid)
   ->select('score')
   ->get()
   ->pluck('score');

Actually you said

But If I include the before-modified array of just the key/value of score / x.. then it returns fine. I just want the score values in an array.

That works because the Eloquent Collections Object has implemented the __toString() magic method to be able to get the string representation of the object so if you try to do this

{{$scores}} //you will get [1,2,3,4,5] 
//(the string representation defined into the __toString method)

But if you try to convert that to an array you will break the function signature because you are trying to pass an array instead of an string

So if you neeed that values converted into a custom string or comma separated values you can append the implode method to your call chain

<?php
$scores = Book::where('author_id', '=', $uid)
   ->select('score')
   ->get()
   ->pluck('score')
   ->implode(',');

Now if you use {{$scores}} in your view you will print '1,2,3,4,5,6'

2 Comments

Actually the pluck method runs itself a foreach.
Sure, but if you use laravel you would try to be "eloquent"

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.