Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adaykin's avatar

Database results to string

Hello people!

So I'm using blade and have a homepage that includes a partial (phone-box.blade.php)

I'm using a loop on the homepage so that the phone-box appears 16 times:

 @for($i = 0; $i < 16; $i++)
      @include('global.phones.partials.phone-box')
 @endfor

This works as intended. Now I have a database query that retrieves 16 numbers

PagesController.php:

public function home()
    {
        $generatedNumbers= DB::select("select number from number_pool where prefix = '01' LIMIT 16");
        return view('justlocal.pages.home', ['generatedNumbers' => $generatedNumbers]);
    }

Now this returns the correct results if I use var_dump in the partial using var_dump($generatedNumbers);

However, I am now attempting to figure out how to make it so that each 'phone-box' contains 1 number each. I imagine it's something simple but I am currently having no luck finding a solution.

Any help would be appreciated!

0 likes
15 replies
bestmomo's avatar

Maybe include partial and send data :

@foreach ($generatedNumbers as $number)
    @include('global.phones.partials.phone-box', $number)
@endforeach

Not sure number is an array in your case...

adaykin's avatar

@bestmomo I believe I tried that method before. I tried again and recieved the following error:

ErrorException in Factory.php line 153: array_merge(): Argument #2 is not an array

bestmomo's avatar

@adaykin what do you get in $number ?

Also I dont think your query is working. Should be better with something like this :

$generatedNumbers = DB::table('number_pool')
->select('number')
->wherePrefix('01')
->take(16)
->get();
adaykin's avatar

@bestmomo Let me know if I'm grabbing the wrong end of the stick here, but I feel that I should clarify that 'number' is the column name and as such 'email as user_email' is not a column in my database table.

Even if I change it to something like:

$generatedNumbers = DB::table('number_pool')->selectRaw('number')->take(16)->get();

I still recieve the array error.

bestmomo's avatar

@adaykin sorry, I have updated my query.

The array error is because $number is not an array.

Try that :

@foreach ($generatedNumbers as $number)
    @include('global.phones.partials.phone-box', compact('number'))
@endforeach 
adaykin's avatar

@bestmomo No worries :)

Right, well I'm no longer recieving any errors so it seems that those two files are fine now. In the partial itself, however, it's throwing errors. I'm probably being a bit dim (again!), but I'm not sure what to put here to output the number.

EDIT: I managed to get the results to display by using first() instead of get(). This obviously does have the downside of all my phone boxes displaying the same result. If I use get() instead I recieve the following error:

Trying to get property of non-object

adaykin's avatar

@bestmomo Thank for your patience, currently 'number' is being treated as an undefined variable in the error message. I think my partial may be wrong?

<div class="phone-number">
    <h4>CAMBERLEY</h4>

    <h3><span class="number hilite hilite-primary">
    <?php
        $number['number']
    ?>
    </span></h3>
    <h4>SETUP: <span class="hilite hilite-primary">FREE</span>
        <span></span><a class="btn btn-primary btn-add" href="#">
            <i class="fa fa-shopping-cart fa-1x"></i> add</a></h4>
</div>

EDIT: Nevermind I messed something up. It doesnt reutnr with an undefined variable error - it returns with a different error: Call to a member function toArray() on a non-object

bestmomo's avatar
Level 52

@adaykin

What do you get with :

$generatedNumbers = DB::table('number_pool')
->select('number')
->wherePrefix('01')
->take(16)
->get();

dd($generatedNumbers);
adaykin's avatar

@bestmomo this:

array:16 [▼ 0 => {#176 ▼ +"number": "01430621998" } 1 => {#177 ▼ +"number": "01430621997" } 2 => {#178 ▼ +"number": "01430621996" } 3 => {#179 ▼ +"number": "01430621995" } 4 => {#180 ▼ +"number": "01430621994" } 5 => {#181 ▼ +"number": "01430621993" } 6 => {#182 ▼ +"number": "01430621992" } 7 => {#183 ▼ +"number": "01430621989" } 8 => {#184 ▼ +"number": "01430621987" } 9 => {#185 ▼ +"number": "01430621986" } 10 => {#186 ▼ +"number": "01430621985" } 11 => {#187 ▼ +"number": "01430621984" } 12 => {#188 ▼ +"number": "01430621983" } 13 => {#189 ▼ +"number": "01430621982" } 14 => {#190 ▼ +"number": "01430621981" } 15 => {#191 ▼ +"number": "01430621979" } ]

Which seems correct no?

EDIT:

Fixed it! thanks a lot for your help! Now all I need is to randomise the results and im done!

bestmomo's avatar

@adaykin

Oh ! As I always use Eloquent I get collection but as you use only query builder you get an array. When you make a loop in this array you get objects. As you need an array you must convert in array as I told you first :

@foreach ($generatedNumbers as $number)
    @include('global.phones.partials.phone-box', compact('number'))
@endforeach

Now in the loop you get an array with key 'number', so to read it in partial :

$number['number']->number;

But it would be easier to use Eloquent.

willvincent's avatar

Now all I need is to randomise the results

Try this:

$generatedNumbers = DB::table('number_pool')
->select('number')
->wherePrefix('01')
->orderBy(DB::raw("RAND()"))
->take(16)
->get();
bestmomo's avatar

Or this direct syntax:

->orderByRaw("RAND()")
adaykin's avatar

Unfortunatly that slows the page down considerably (its a big database)

I'm currently attempting to use php's random generating feature to randomly generate query conditions. I'll let you know if I have any sucess

Please or to participate in this conversation.