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;








0















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?










share|improve this question
























  • 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

















0















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?










share|improve this question
























  • 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













0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















0














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 :)






share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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 :)






    share|improve this answer



























      0














      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 :)






      share|improve this answer

























        0












        0








        0







        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 :)






        share|improve this answer













        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 :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 17:45









        Florian LaforgueFlorian Laforgue

        1565




        1565





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현