use IFNULL in laravel












6















I have a query in laravel which is working fine.



$subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', 'downloads.is_download' )
->orderBy('subjectID')
->get();


There is only one issue that is_download comes null when there is no relative entry in table. After some research I found that there is a function IFNULL by using which I can change is_download null to 0. So here is my query which is not working. Blank screen is showing.



$subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', IFNULL( `downloads`.`is_download` , 0 ) )
->orderBy('subjectID')
->get();


I am able to write this query in my phpmyadmin but not know how to write in laravel



This is the api, So whole code looks like



use IlluminateDatabaseQueryExpression as raw;
use projectModelsSubject;
use projectModelsSemester;
use projectModelsStudentRegistration;

$app->get('/api/getSubject/:studentID', function($studentID) use ($app) {
error_reporting(E_ALL);
$currentUser = StudentRegistration::where('studentID', $studentID)->first();

$subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', IFNULL( `downloads`.`is_download` , 0 ) )
->orderBy('subjectID')
->get();
print_r($subjects);
return $app->response->write(json_encode([
'error' => 0,
'subjects' => $subjects
]));
});









share|improve this question





























    6















    I have a query in laravel which is working fine.



    $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
    ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
    ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', 'downloads.is_download' )
    ->orderBy('subjectID')
    ->get();


    There is only one issue that is_download comes null when there is no relative entry in table. After some research I found that there is a function IFNULL by using which I can change is_download null to 0. So here is my query which is not working. Blank screen is showing.



    $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
    ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
    ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', IFNULL( `downloads`.`is_download` , 0 ) )
    ->orderBy('subjectID')
    ->get();


    I am able to write this query in my phpmyadmin but not know how to write in laravel



    This is the api, So whole code looks like



    use IlluminateDatabaseQueryExpression as raw;
    use projectModelsSubject;
    use projectModelsSemester;
    use projectModelsStudentRegistration;

    $app->get('/api/getSubject/:studentID', function($studentID) use ($app) {
    error_reporting(E_ALL);
    $currentUser = StudentRegistration::where('studentID', $studentID)->first();

    $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
    ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
    ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', IFNULL( `downloads`.`is_download` , 0 ) )
    ->orderBy('subjectID')
    ->get();
    print_r($subjects);
    return $app->response->write(json_encode([
    'error' => 0,
    'subjects' => $subjects
    ]));
    });









    share|improve this question



























      6












      6








      6








      I have a query in laravel which is working fine.



      $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
      ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
      ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', 'downloads.is_download' )
      ->orderBy('subjectID')
      ->get();


      There is only one issue that is_download comes null when there is no relative entry in table. After some research I found that there is a function IFNULL by using which I can change is_download null to 0. So here is my query which is not working. Blank screen is showing.



      $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
      ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
      ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', IFNULL( `downloads`.`is_download` , 0 ) )
      ->orderBy('subjectID')
      ->get();


      I am able to write this query in my phpmyadmin but not know how to write in laravel



      This is the api, So whole code looks like



      use IlluminateDatabaseQueryExpression as raw;
      use projectModelsSubject;
      use projectModelsSemester;
      use projectModelsStudentRegistration;

      $app->get('/api/getSubject/:studentID', function($studentID) use ($app) {
      error_reporting(E_ALL);
      $currentUser = StudentRegistration::where('studentID', $studentID)->first();

      $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
      ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
      ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', IFNULL( `downloads`.`is_download` , 0 ) )
      ->orderBy('subjectID')
      ->get();
      print_r($subjects);
      return $app->response->write(json_encode([
      'error' => 0,
      'subjects' => $subjects
      ]));
      });









      share|improve this question
















      I have a query in laravel which is working fine.



      $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
      ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
      ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', 'downloads.is_download' )
      ->orderBy('subjectID')
      ->get();


      There is only one issue that is_download comes null when there is no relative entry in table. After some research I found that there is a function IFNULL by using which I can change is_download null to 0. So here is my query which is not working. Blank screen is showing.



      $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
      ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
      ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', IFNULL( `downloads`.`is_download` , 0 ) )
      ->orderBy('subjectID')
      ->get();


      I am able to write this query in my phpmyadmin but not know how to write in laravel



      This is the api, So whole code looks like



      use IlluminateDatabaseQueryExpression as raw;
      use projectModelsSubject;
      use projectModelsSemester;
      use projectModelsStudentRegistration;

      $app->get('/api/getSubject/:studentID', function($studentID) use ($app) {
      error_reporting(E_ALL);
      $currentUser = StudentRegistration::where('studentID', $studentID)->first();

      $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
      ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
      ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', IFNULL( `downloads`.`is_download` , 0 ) )
      ->orderBy('subjectID')
      ->get();
      print_r($subjects);
      return $app->response->write(json_encode([
      'error' => 0,
      'subjects' => $subjects
      ]));
      });






      php mysql sql database laravel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 '16 at 12:17







      Kapil Sharma

















      asked Mar 22 '16 at 11:19









      Kapil SharmaKapil Sharma

      3551516




      3551516
























          2 Answers
          2






          active

          oldest

          votes


















          4














          Try like this:



          DB::Raw('IFNULL( `downloads`.`is_download` , 0 )');


          $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
          ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
          ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', DB::Raw('IFNULL( `downloads`.`is_download` , 0 )') )
          ->orderBy('subjectID')
          ->get();





          share|improve this answer
























          • blank screen. no change.

            – Kapil Sharma
            Mar 22 '16 at 11:38











          • Did you use DB::table() instead of $app->db->table() and DB::Raw() ? This should work fine. Otherwise check your query again when you are using it in laravel.

            – Md Mahfuzur Rahman
            Mar 22 '16 at 12:28











          • whats wrong in using $app->db->table()?

            – Kapil Sharma
            Mar 22 '16 at 12:29











          • In laravel documentation, I didn't see anything like this. So, it's better to use DB::table().

            – Md Mahfuzur Rahman
            Mar 22 '16 at 12:38



















          0














          You just need to use like this way,



          DB::raw('IFNULL( downloads.is_download, 0) as is_download')





          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%2f36153019%2fuse-ifnull-in-laravel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            Try like this:



            DB::Raw('IFNULL( `downloads`.`is_download` , 0 )');


            $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
            ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
            ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', DB::Raw('IFNULL( `downloads`.`is_download` , 0 )') )
            ->orderBy('subjectID')
            ->get();





            share|improve this answer
























            • blank screen. no change.

              – Kapil Sharma
              Mar 22 '16 at 11:38











            • Did you use DB::table() instead of $app->db->table() and DB::Raw() ? This should work fine. Otherwise check your query again when you are using it in laravel.

              – Md Mahfuzur Rahman
              Mar 22 '16 at 12:28











            • whats wrong in using $app->db->table()?

              – Kapil Sharma
              Mar 22 '16 at 12:29











            • In laravel documentation, I didn't see anything like this. So, it's better to use DB::table().

              – Md Mahfuzur Rahman
              Mar 22 '16 at 12:38
















            4














            Try like this:



            DB::Raw('IFNULL( `downloads`.`is_download` , 0 )');


            $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
            ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
            ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', DB::Raw('IFNULL( `downloads`.`is_download` , 0 )') )
            ->orderBy('subjectID')
            ->get();





            share|improve this answer
























            • blank screen. no change.

              – Kapil Sharma
              Mar 22 '16 at 11:38











            • Did you use DB::table() instead of $app->db->table() and DB::Raw() ? This should work fine. Otherwise check your query again when you are using it in laravel.

              – Md Mahfuzur Rahman
              Mar 22 '16 at 12:28











            • whats wrong in using $app->db->table()?

              – Kapil Sharma
              Mar 22 '16 at 12:29











            • In laravel documentation, I didn't see anything like this. So, it's better to use DB::table().

              – Md Mahfuzur Rahman
              Mar 22 '16 at 12:38














            4












            4








            4







            Try like this:



            DB::Raw('IFNULL( `downloads`.`is_download` , 0 )');


            $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
            ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
            ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', DB::Raw('IFNULL( `downloads`.`is_download` , 0 )') )
            ->orderBy('subjectID')
            ->get();





            share|improve this answer













            Try like this:



            DB::Raw('IFNULL( `downloads`.`is_download` , 0 )');


            $subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
            ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
            ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price', DB::Raw('IFNULL( `downloads`.`is_download` , 0 )') )
            ->orderBy('subjectID')
            ->get();






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 22 '16 at 11:33









            Md Mahfuzur RahmanMd Mahfuzur Rahman

            1,81921220




            1,81921220













            • blank screen. no change.

              – Kapil Sharma
              Mar 22 '16 at 11:38











            • Did you use DB::table() instead of $app->db->table() and DB::Raw() ? This should work fine. Otherwise check your query again when you are using it in laravel.

              – Md Mahfuzur Rahman
              Mar 22 '16 at 12:28











            • whats wrong in using $app->db->table()?

              – Kapil Sharma
              Mar 22 '16 at 12:29











            • In laravel documentation, I didn't see anything like this. So, it's better to use DB::table().

              – Md Mahfuzur Rahman
              Mar 22 '16 at 12:38



















            • blank screen. no change.

              – Kapil Sharma
              Mar 22 '16 at 11:38











            • Did you use DB::table() instead of $app->db->table() and DB::Raw() ? This should work fine. Otherwise check your query again when you are using it in laravel.

              – Md Mahfuzur Rahman
              Mar 22 '16 at 12:28











            • whats wrong in using $app->db->table()?

              – Kapil Sharma
              Mar 22 '16 at 12:29











            • In laravel documentation, I didn't see anything like this. So, it's better to use DB::table().

              – Md Mahfuzur Rahman
              Mar 22 '16 at 12:38

















            blank screen. no change.

            – Kapil Sharma
            Mar 22 '16 at 11:38





            blank screen. no change.

            – Kapil Sharma
            Mar 22 '16 at 11:38













            Did you use DB::table() instead of $app->db->table() and DB::Raw() ? This should work fine. Otherwise check your query again when you are using it in laravel.

            – Md Mahfuzur Rahman
            Mar 22 '16 at 12:28





            Did you use DB::table() instead of $app->db->table() and DB::Raw() ? This should work fine. Otherwise check your query again when you are using it in laravel.

            – Md Mahfuzur Rahman
            Mar 22 '16 at 12:28













            whats wrong in using $app->db->table()?

            – Kapil Sharma
            Mar 22 '16 at 12:29





            whats wrong in using $app->db->table()?

            – Kapil Sharma
            Mar 22 '16 at 12:29













            In laravel documentation, I didn't see anything like this. So, it's better to use DB::table().

            – Md Mahfuzur Rahman
            Mar 22 '16 at 12:38





            In laravel documentation, I didn't see anything like this. So, it's better to use DB::table().

            – Md Mahfuzur Rahman
            Mar 22 '16 at 12:38













            0














            You just need to use like this way,



            DB::raw('IFNULL( downloads.is_download, 0) as is_download')





            share|improve this answer




























              0














              You just need to use like this way,



              DB::raw('IFNULL( downloads.is_download, 0) as is_download')





              share|improve this answer


























                0












                0








                0







                You just need to use like this way,



                DB::raw('IFNULL( downloads.is_download, 0) as is_download')





                share|improve this answer













                You just need to use like this way,



                DB::raw('IFNULL( downloads.is_download, 0) as is_download')






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 29 '18 at 8:42









                Dayachand PatelDayachand Patel

                224210




                224210






























                    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%2f36153019%2fuse-ifnull-in-laravel%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

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas