Laravel and MySQL stored procedure results

I am dealing with some really old code that I am trying to run on Laravel. The database is complex and filled with unnecessary long-stored procedures for which I do not have a budget for rewriting.

For the most part, they work well, as I can access unique results, for example $ myID = $ result [0] โ†’ id

I hope this is more than a knowledge gap in PHP ...

$result = DB::select(DB::raw("Call MyOldStoredProcedure()"));
print_r($result);

This gives me:

Array ( [0] => stdClass Object ( [MIN(user_responses.sectionid)] => 2 ) )

Sorry, I cannot access

$number = $result[0]["MIN(user_responses.sectionid)"]; //or...
$number = $result[0]->...

What can I do to get this unique result from this weird associative array / object? I am going to refuse and parse an array string, but I know the best way.

+4
1

- , :

$number = $result[0]->{'MIN(user_responses.sectionid)'};

- , :

$property = 'MIN(user_responses.sectionid)';
$number = $result[0]->$property;

, , .

$array = (array)$result[0];
$number = $array['MIN(user_responses.sectionid)'];

// or, if you're on PHP 7+
$number = ((array)$result[0])['MIN(user_responses.sectionid)'];
+1

Source: https://habr.com/ru/post/1664227/


All Articles