Laravel db::select() returns one string resultHow to output MySQL query results in CSV format?How do I add indices to MySQL tables?Reference - What does this error mean in PHP?Laravel 4: DB::select with IN statmentLaravel 5 – Remove Public from URLResponse::json() doesn't return anything with Nginx + laravel 4laravel terminable middleware gets called before returning responseLaravel created_at return object in place of date format in database?phpunit testing resource laravel 5.5 returns collection instead a jsonLaravel how to return selected column with auth:user()
How much water is needed to create a Katana capable of cutting flesh, bones and wood?
What happens if you do emergency landing on a US base in middle of the ocean?
Can you please explain this joke: "I'm going bananas is what I tell my bananas before I leave the house"?
What is the right way to float a home lab?
What flavor of zksnark in tezos
Why is Colorado so different politically from nearby states?
What's the correct term for a waitress in the Middle Ages?
What does it mean by "d-ism of Leibniz" and "dotage of Newton" in simple English?
Chopin: marche funèbre bar 15 impossible place
Metal bar on DMM PCB
correct term describing the action of sending a brand-new ship out into its first seafaring trip
Explain Ant-Man's "not it" scene from Avengers: Endgame
Short story written from alien perspective with this line: "It's too bright to look at, so they don't"
Do manufacturers try make their components as close to ideal ones as possible?
What are they doing to this poor rocket?
Old black and white movie: glowing black rocks slowly turn you into stone upon touch
You've spoiled/damaged the card
Will TSA allow me to carry a Continuous Positive Airway Pressure (CPAP) device?
How were concentration and extermination camp guards recruited?
Why don't B747s start takeoffs with full throttle?
Does any lore text explain why the planes of Acheron, Gehenna, and Carceri are the alignment they are?
Secure offsite backup, even in the case of hacker root access
Poisson distribution: why does time between events follow an exponential distribution?
Does Peach's float negate shorthop knockback multipliers?
Laravel db::select() returns one string result
How to output MySQL query results in CSV format?How do I add indices to MySQL tables?Reference - What does this error mean in PHP?Laravel 4: DB::select with IN statmentLaravel 5 – Remove Public from URLResponse::json() doesn't return anything with Nginx + laravel 4laravel terminable middleware gets called before returning responseLaravel created_at return object in place of date format in database?phpunit testing resource laravel 5.5 returns collection instead a jsonLaravel how to return selected column with auth:user()
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to make a select using laravel and return with a response-json, i try:
public function verificaQuantidadeAnimaisAprovar()
$quantidade_pets_aprovar = DB::select('SELECT (SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_perdidos)
FROM DUAL '
);
return response()->json($quantidade_pets_aprovar, 201);
In the bd i have this result when i run this query:
but when i run my api in postman i receive:
["(SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +rn
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
rn (SELECT SUM(animal_pendente) FROM
cademeupet.animais_perdidos)":"36"]
I need only the number result query: 36.
How i can fix this?
mysql laravel
add a comment |
I'm trying to make a select using laravel and return with a response-json, i try:
public function verificaQuantidadeAnimaisAprovar()
$quantidade_pets_aprovar = DB::select('SELECT (SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_perdidos)
FROM DUAL '
);
return response()->json($quantidade_pets_aprovar, 201);
In the bd i have this result when i run this query:
but when i run my api in postman i receive:
["(SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +rn
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
rn (SELECT SUM(animal_pendente) FROM
cademeupet.animais_perdidos)":"36"]
I need only the number result query: 36.
How i can fix this?
mysql laravel
Take a look at Raw Expressions - laravel.com/docs/5.8/queries#raw-expressions
– senty
Mar 24 at 13:27
any example using query builder? this examples is using eloquent, right?
– renaottttpls
Mar 24 at 13:28
SymfonyComponentDebugExceptionFatalThrowableError: Call to a member function toSql() on array in file
– renaottttpls
Mar 24 at 13:33
add a comment |
I'm trying to make a select using laravel and return with a response-json, i try:
public function verificaQuantidadeAnimaisAprovar()
$quantidade_pets_aprovar = DB::select('SELECT (SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_perdidos)
FROM DUAL '
);
return response()->json($quantidade_pets_aprovar, 201);
In the bd i have this result when i run this query:
but when i run my api in postman i receive:
["(SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +rn
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
rn (SELECT SUM(animal_pendente) FROM
cademeupet.animais_perdidos)":"36"]
I need only the number result query: 36.
How i can fix this?
mysql laravel
I'm trying to make a select using laravel and return with a response-json, i try:
public function verificaQuantidadeAnimaisAprovar()
$quantidade_pets_aprovar = DB::select('SELECT (SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_perdidos)
FROM DUAL '
);
return response()->json($quantidade_pets_aprovar, 201);
In the bd i have this result when i run this query:
but when i run my api in postman i receive:
["(SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +rn
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
rn (SELECT SUM(animal_pendente) FROM
cademeupet.animais_perdidos)":"36"]
I need only the number result query: 36.
How i can fix this?
mysql laravel
mysql laravel
edited Mar 24 at 13:53
Ross Wilson
18.7k33043
18.7k33043
asked Mar 24 at 13:21
renaottttplsrenaottttpls
11
11
Take a look at Raw Expressions - laravel.com/docs/5.8/queries#raw-expressions
– senty
Mar 24 at 13:27
any example using query builder? this examples is using eloquent, right?
– renaottttpls
Mar 24 at 13:28
SymfonyComponentDebugExceptionFatalThrowableError: Call to a member function toSql() on array in file
– renaottttpls
Mar 24 at 13:33
add a comment |
Take a look at Raw Expressions - laravel.com/docs/5.8/queries#raw-expressions
– senty
Mar 24 at 13:27
any example using query builder? this examples is using eloquent, right?
– renaottttpls
Mar 24 at 13:28
SymfonyComponentDebugExceptionFatalThrowableError: Call to a member function toSql() on array in file
– renaottttpls
Mar 24 at 13:33
Take a look at Raw Expressions - laravel.com/docs/5.8/queries#raw-expressions
– senty
Mar 24 at 13:27
Take a look at Raw Expressions - laravel.com/docs/5.8/queries#raw-expressions
– senty
Mar 24 at 13:27
any example using query builder? this examples is using eloquent, right?
– renaottttpls
Mar 24 at 13:28
any example using query builder? this examples is using eloquent, right?
– renaottttpls
Mar 24 at 13:28
SymfonyComponentDebugExceptionFatalThrowableError: Call to a member function toSql() on array in file
– renaottttpls
Mar 24 at 13:33
SymfonyComponentDebugExceptionFatalThrowableError: Call to a member function toSql() on array in file
– renaottttpls
Mar 24 at 13:33
add a comment |
1 Answer
1
active
oldest
votes
You are passing a whole SQL expression inside of the select
method, which is designed to perform a select sql operation.
You should probably use the raw expression which is designed to handle complete sql queries :
public function verificaQuantidadeAnimaisAprovar()
$quantidade_pets_aprovar = DB::raw('SELECT (SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_perdidos)
FROM DUAL '
);
return response()->json($quantidade_pets_aprovar, 200);
Note : I also changed the http code of your response because 201
(Created) indicates the creation of something, which does not seem to happen here. 200
(Ok) seems more appropriate. Be free to put 201
back if this operation creates something in your database.
Let me know if it helped you :)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324223%2flaravel-dbselect-returns-one-string-result%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are passing a whole SQL expression inside of the select
method, which is designed to perform a select sql operation.
You should probably use the raw expression which is designed to handle complete sql queries :
public function verificaQuantidadeAnimaisAprovar()
$quantidade_pets_aprovar = DB::raw('SELECT (SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_perdidos)
FROM DUAL '
);
return response()->json($quantidade_pets_aprovar, 200);
Note : I also changed the http code of your response because 201
(Created) indicates the creation of something, which does not seem to happen here. 200
(Ok) seems more appropriate. Be free to put 201
back if this operation creates something in your database.
Let me know if it helped you :)
add a comment |
You are passing a whole SQL expression inside of the select
method, which is designed to perform a select sql operation.
You should probably use the raw expression which is designed to handle complete sql queries :
public function verificaQuantidadeAnimaisAprovar()
$quantidade_pets_aprovar = DB::raw('SELECT (SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_perdidos)
FROM DUAL '
);
return response()->json($quantidade_pets_aprovar, 200);
Note : I also changed the http code of your response because 201
(Created) indicates the creation of something, which does not seem to happen here. 200
(Ok) seems more appropriate. Be free to put 201
back if this operation creates something in your database.
Let me know if it helped you :)
add a comment |
You are passing a whole SQL expression inside of the select
method, which is designed to perform a select sql operation.
You should probably use the raw expression which is designed to handle complete sql queries :
public function verificaQuantidadeAnimaisAprovar()
$quantidade_pets_aprovar = DB::raw('SELECT (SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_perdidos)
FROM DUAL '
);
return response()->json($quantidade_pets_aprovar, 200);
Note : I also changed the http code of your response because 201
(Created) indicates the creation of something, which does not seem to happen here. 200
(Ok) seems more appropriate. Be free to put 201
back if this operation creates something in your database.
Let me know if it helped you :)
You are passing a whole SQL expression inside of the select
method, which is designed to perform a select sql operation.
You should probably use the raw expression which is designed to handle complete sql queries :
public function verificaQuantidadeAnimaisAprovar()
$quantidade_pets_aprovar = DB::raw('SELECT (SELECT SUM(animal_pendente) FROM cademeupet.animais_adocao) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_encontrados) +
(SELECT SUM(animal_pendente) FROM cademeupet.animais_perdidos)
FROM DUAL '
);
return response()->json($quantidade_pets_aprovar, 200);
Note : I also changed the http code of your response because 201
(Created) indicates the creation of something, which does not seem to happen here. 200
(Ok) seems more appropriate. Be free to put 201
back if this operation creates something in your database.
Let me know if it helped you :)
answered Mar 24 at 17:45
Florian LaforgueFlorian Laforgue
1565
1565
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324223%2flaravel-dbselect-returns-one-string-result%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Take a look at Raw Expressions - laravel.com/docs/5.8/queries#raw-expressions
– senty
Mar 24 at 13:27
any example using query builder? this examples is using eloquent, right?
– renaottttpls
Mar 24 at 13:28
SymfonyComponentDebugExceptionFatalThrowableError: Call to a member function toSql() on array in file
– renaottttpls
Mar 24 at 13:33