Home > Software engineering >  Array to string conversion issue on laravel 9
Array to string conversion issue on laravel 9

Time:11-28

When I try to print what is coming from my query to SQLite I get this error, but there is only one possible answer to the query. So, why is it returning an array?

$rGDE = DB::select('select "' . $gestiondeempresas . '" from competencias2 where nombre == "' . $username . '"');

print($rGDE);

I was hoping to get a number, not an array. I tried searching for a solution and I discovered that if I use:

$pritnableRaw = str_replace("'", "\'", json_encode($rGDE));

print($pritnableRaw);

I get:

[{"gestiondeempresas":"0"}]

Which is what I want to achieve, but I don't know how to get only the number from there.

* I'm using Laravel latest version (9.41.0) and SQlite database.

CodePudding user response:

I hope I have understood your question and intention correctly, and my apologies if I have not.

Using DB::select(...args) method will return an array, as indicated in the function documentation:

    /**
     * Run a select statement against the database.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @param  bool  $useReadPdo
     * @return array
     */

There are three other options to help you achieve what you want. My personal preference lies with option 3, however it is down to you and what you decide best meets your need.

[ ] Option 1

If you wish to get a single record, you can use DB::selectOne(...args) which will give you one row, rather than an array of rows.

However,this will still not be a single value. You'd have to do then access the column you selected from the resulting array. Eg.

<?php
$result = DB::selectOne($myQuery);
// Results = ['my_column' => 0]
$value = $result['my_column'];
print($value);

// > 0

[ ] Option 2

There is actually a method that handles this extra step for you: the scalar function

By doing DB::scalar($query);, you will get the value of first column from the first result. This means, so long as you are selecting the correct column as in your query, you will get the value of that column.

<?php
$rGDE = DB::scalar('select "'.$gestiondeempresas.'" from competencias2 where nombre == "'.$username.'"');

print($rGDE);
// > 0

[x] Option 3

The above options follow the same style of what your own personal solution is so far. However,* I would encourage you to use Eloquent as much as possible.

If you have a model defined for your competencias2 table, you could instead build your query like this:

<?php

$competencias = App\Models\Competencias::where('nombre', $username)->first();

$rGDE = $competencias->gestiondeempresas;

Eloquent is a very powerful tool that Laravel offers, and allows you to do some pretty cool things. If you wanted to get an array of ALL of the values that were returned from the query, you could change it up to this:

<?php
$competenciasCollection = App\Models\Competencias::where('nombre', $username)->get();

$competenciasCollection->pluck('gestiondeempresas');
// [0,1,2, etc.]

*this is opinionated

  • Related