Manipulating data within an object












1















Hi I am new to Laravel and php. In my database each student has three marks on each subject and I need to retrieve them and send to the view the average mark for each student.
I tried doing it like below, but the returned value is an object (e.g. [5,4,3]) and it doesn't let me count the average. Please advise how I can operate with data within the object.



$students = Student::all();

foreach ($students as $student) {
$mathPoints = Point:: where('subject_id', 1)
->where('student_id', $student->id)
->pluck('points');
}


I tried turning it into an array by (array) method, but I couldn't calculate the sum of values with array_sum after.



Update: my Point model:



namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Point extends Model
{
//Get the student the credit points are related to
public function student()
{
return $this->belongsTo('App/Models/Student');
}
//Get the subject the credit points are related to
public function subject()
{
return $this->belongsTo('App/Models/Subject');
}
}









share|improve this question

























  • can you please post your Point model?

    – JoseLinares
    Nov 19 '18 at 9:38











  • @LiMi add that to the question

    – user10415043
    Nov 19 '18 at 9:40











  • @YashKaranke added

    – LiMi
    Nov 19 '18 at 9:44











  • Probably not part of the solution, but you have an errant space in Point:: where

    – Adam Hopkinson
    Nov 19 '18 at 9:44
















1















Hi I am new to Laravel and php. In my database each student has three marks on each subject and I need to retrieve them and send to the view the average mark for each student.
I tried doing it like below, but the returned value is an object (e.g. [5,4,3]) and it doesn't let me count the average. Please advise how I can operate with data within the object.



$students = Student::all();

foreach ($students as $student) {
$mathPoints = Point:: where('subject_id', 1)
->where('student_id', $student->id)
->pluck('points');
}


I tried turning it into an array by (array) method, but I couldn't calculate the sum of values with array_sum after.



Update: my Point model:



namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Point extends Model
{
//Get the student the credit points are related to
public function student()
{
return $this->belongsTo('App/Models/Student');
}
//Get the subject the credit points are related to
public function subject()
{
return $this->belongsTo('App/Models/Subject');
}
}









share|improve this question

























  • can you please post your Point model?

    – JoseLinares
    Nov 19 '18 at 9:38











  • @LiMi add that to the question

    – user10415043
    Nov 19 '18 at 9:40











  • @YashKaranke added

    – LiMi
    Nov 19 '18 at 9:44











  • Probably not part of the solution, but you have an errant space in Point:: where

    – Adam Hopkinson
    Nov 19 '18 at 9:44














1












1








1








Hi I am new to Laravel and php. In my database each student has three marks on each subject and I need to retrieve them and send to the view the average mark for each student.
I tried doing it like below, but the returned value is an object (e.g. [5,4,3]) and it doesn't let me count the average. Please advise how I can operate with data within the object.



$students = Student::all();

foreach ($students as $student) {
$mathPoints = Point:: where('subject_id', 1)
->where('student_id', $student->id)
->pluck('points');
}


I tried turning it into an array by (array) method, but I couldn't calculate the sum of values with array_sum after.



Update: my Point model:



namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Point extends Model
{
//Get the student the credit points are related to
public function student()
{
return $this->belongsTo('App/Models/Student');
}
//Get the subject the credit points are related to
public function subject()
{
return $this->belongsTo('App/Models/Subject');
}
}









share|improve this question
















Hi I am new to Laravel and php. In my database each student has three marks on each subject and I need to retrieve them and send to the view the average mark for each student.
I tried doing it like below, but the returned value is an object (e.g. [5,4,3]) and it doesn't let me count the average. Please advise how I can operate with data within the object.



$students = Student::all();

foreach ($students as $student) {
$mathPoints = Point:: where('subject_id', 1)
->where('student_id', $student->id)
->pluck('points');
}


I tried turning it into an array by (array) method, but I couldn't calculate the sum of values with array_sum after.



Update: my Point model:



namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Point extends Model
{
//Get the student the credit points are related to
public function student()
{
return $this->belongsTo('App/Models/Student');
}
//Get the subject the credit points are related to
public function subject()
{
return $this->belongsTo('App/Models/Subject');
}
}






php laravel-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 10:21









zedling

461220




461220










asked Nov 19 '18 at 9:36









LiMiLiMi

467




467













  • can you please post your Point model?

    – JoseLinares
    Nov 19 '18 at 9:38











  • @LiMi add that to the question

    – user10415043
    Nov 19 '18 at 9:40











  • @YashKaranke added

    – LiMi
    Nov 19 '18 at 9:44











  • Probably not part of the solution, but you have an errant space in Point:: where

    – Adam Hopkinson
    Nov 19 '18 at 9:44



















  • can you please post your Point model?

    – JoseLinares
    Nov 19 '18 at 9:38











  • @LiMi add that to the question

    – user10415043
    Nov 19 '18 at 9:40











  • @YashKaranke added

    – LiMi
    Nov 19 '18 at 9:44











  • Probably not part of the solution, but you have an errant space in Point:: where

    – Adam Hopkinson
    Nov 19 '18 at 9:44

















can you please post your Point model?

– JoseLinares
Nov 19 '18 at 9:38





can you please post your Point model?

– JoseLinares
Nov 19 '18 at 9:38













@LiMi add that to the question

– user10415043
Nov 19 '18 at 9:40





@LiMi add that to the question

– user10415043
Nov 19 '18 at 9:40













@YashKaranke added

– LiMi
Nov 19 '18 at 9:44





@YashKaranke added

– LiMi
Nov 19 '18 at 9:44













Probably not part of the solution, but you have an errant space in Point:: where

– Adam Hopkinson
Nov 19 '18 at 9:44





Probably not part of the solution, but you have an errant space in Point:: where

– Adam Hopkinson
Nov 19 '18 at 9:44












2 Answers
2






active

oldest

votes


















1














use Model::avg('columnName') to calculate average .



read more here : https://laravel.com/docs/5.4/queries#aggregates



$students = Student::all();
foreach ($students as $student){
$mathPoints = Point::where(['subject_id'=>1,'student_id'=>$student->id])->avg('points');
$student->avgPoint=$mathPoints;
}


inside your blade :



{{ $student->avgPoint}}





share|improve this answer

































    1














    Point::where('student_id', $student->id)->get()


    When you use all() with Eloquent, it makes the query for you. If you use where(), you have to ‘get’ it using get() before you can use it as a collection.



    $mathPoints = Point::where('student_id', $student->id)
    ->get()
    ->pluck('points');


    However, I’d probably look at using more complicated queries to fetch this data, as you could easily end up making hundreds of queries per page, rather than just 1.






    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%2f53371784%2fmanipulating-data-within-an-object%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









      1














      use Model::avg('columnName') to calculate average .



      read more here : https://laravel.com/docs/5.4/queries#aggregates



      $students = Student::all();
      foreach ($students as $student){
      $mathPoints = Point::where(['subject_id'=>1,'student_id'=>$student->id])->avg('points');
      $student->avgPoint=$mathPoints;
      }


      inside your blade :



      {{ $student->avgPoint}}





      share|improve this answer






























        1














        use Model::avg('columnName') to calculate average .



        read more here : https://laravel.com/docs/5.4/queries#aggregates



        $students = Student::all();
        foreach ($students as $student){
        $mathPoints = Point::where(['subject_id'=>1,'student_id'=>$student->id])->avg('points');
        $student->avgPoint=$mathPoints;
        }


        inside your blade :



        {{ $student->avgPoint}}





        share|improve this answer




























          1












          1








          1







          use Model::avg('columnName') to calculate average .



          read more here : https://laravel.com/docs/5.4/queries#aggregates



          $students = Student::all();
          foreach ($students as $student){
          $mathPoints = Point::where(['subject_id'=>1,'student_id'=>$student->id])->avg('points');
          $student->avgPoint=$mathPoints;
          }


          inside your blade :



          {{ $student->avgPoint}}





          share|improve this answer















          use Model::avg('columnName') to calculate average .



          read more here : https://laravel.com/docs/5.4/queries#aggregates



          $students = Student::all();
          foreach ($students as $student){
          $mathPoints = Point::where(['subject_id'=>1,'student_id'=>$student->id])->avg('points');
          $student->avgPoint=$mathPoints;
          }


          inside your blade :



          {{ $student->avgPoint}}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 9:56

























          answered Nov 19 '18 at 9:46









          Saurabh MistrySaurabh Mistry

          3,3451726




          3,3451726

























              1














              Point::where('student_id', $student->id)->get()


              When you use all() with Eloquent, it makes the query for you. If you use where(), you have to ‘get’ it using get() before you can use it as a collection.



              $mathPoints = Point::where('student_id', $student->id)
              ->get()
              ->pluck('points');


              However, I’d probably look at using more complicated queries to fetch this data, as you could easily end up making hundreds of queries per page, rather than just 1.






              share|improve this answer




























                1














                Point::where('student_id', $student->id)->get()


                When you use all() with Eloquent, it makes the query for you. If you use where(), you have to ‘get’ it using get() before you can use it as a collection.



                $mathPoints = Point::where('student_id', $student->id)
                ->get()
                ->pluck('points');


                However, I’d probably look at using more complicated queries to fetch this data, as you could easily end up making hundreds of queries per page, rather than just 1.






                share|improve this answer


























                  1












                  1








                  1







                  Point::where('student_id', $student->id)->get()


                  When you use all() with Eloquent, it makes the query for you. If you use where(), you have to ‘get’ it using get() before you can use it as a collection.



                  $mathPoints = Point::where('student_id', $student->id)
                  ->get()
                  ->pluck('points');


                  However, I’d probably look at using more complicated queries to fetch this data, as you could easily end up making hundreds of queries per page, rather than just 1.






                  share|improve this answer













                  Point::where('student_id', $student->id)->get()


                  When you use all() with Eloquent, it makes the query for you. If you use where(), you have to ‘get’ it using get() before you can use it as a collection.



                  $mathPoints = Point::where('student_id', $student->id)
                  ->get()
                  ->pluck('points');


                  However, I’d probably look at using more complicated queries to fetch this data, as you could easily end up making hundreds of queries per page, rather than just 1.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 9:41









                  Thomas EdwardsThomas Edwards

                  7,32331432




                  7,32331432






























                      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%2f53371784%2fmanipulating-data-within-an-object%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

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)