Manipulating data within an object











up vote
1
down vote

favorite












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









New contributor




LiMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • can you please post your Point model?
    – JoseLinares
    9 hours ago










  • @LiMi add that to the question
    – Yash Karanke
    9 hours ago










  • @YashKaranke added
    – LiMi
    9 hours ago










  • Probably not part of the solution, but you have an errant space in Point:: where
    – Adam Hopkinson
    9 hours ago















up vote
1
down vote

favorite












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









New contributor




LiMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • can you please post your Point model?
    – JoseLinares
    9 hours ago










  • @LiMi add that to the question
    – Yash Karanke
    9 hours ago










  • @YashKaranke added
    – LiMi
    9 hours ago










  • Probably not part of the solution, but you have an errant space in Point:: where
    – Adam Hopkinson
    9 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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









New contributor




LiMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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









New contributor




LiMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




LiMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 8 hours ago









zedling

448220




448220






New contributor




LiMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 9 hours ago









LiMi

83




83




New contributor




LiMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





LiMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






LiMi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • can you please post your Point model?
    – JoseLinares
    9 hours ago










  • @LiMi add that to the question
    – Yash Karanke
    9 hours ago










  • @YashKaranke added
    – LiMi
    9 hours ago










  • Probably not part of the solution, but you have an errant space in Point:: where
    – Adam Hopkinson
    9 hours ago


















  • can you please post your Point model?
    – JoseLinares
    9 hours ago










  • @LiMi add that to the question
    – Yash Karanke
    9 hours ago










  • @YashKaranke added
    – LiMi
    9 hours ago










  • Probably not part of the solution, but you have an errant space in Point:: where
    – Adam Hopkinson
    9 hours ago
















can you please post your Point model?
– JoseLinares
9 hours ago




can you please post your Point model?
– JoseLinares
9 hours ago












@LiMi add that to the question
– Yash Karanke
9 hours ago




@LiMi add that to the question
– Yash Karanke
9 hours ago












@YashKaranke added
– LiMi
9 hours ago




@YashKaranke added
– LiMi
9 hours ago












Probably not part of the solution, but you have an errant space in Point:: where
– Adam Hopkinson
9 hours ago




Probably not part of the solution, but you have an errant space in Point:: where
– Adam Hopkinson
9 hours ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










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






























    up vote
    2
    down vote













    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',
      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
      });


      }
      });






      LiMi is a new contributor. Be nice, and check out our Code of Conduct.










       

      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








      up vote
      1
      down vote



      accepted










      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



























        up vote
        1
        down vote



        accepted










        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

























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          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 8 hours ago

























          answered 8 hours ago









          Saurabh Mistry

          2,6691726




          2,6691726
























              up vote
              2
              down vote













              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

























                up vote
                2
                down vote













                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























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  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 9 hours ago









                  Thomas Edwards

                  7,09031332




                  7,09031332






















                      LiMi is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      LiMi is a new contributor. Be nice, and check out our Code of Conduct.













                      LiMi is a new contributor. Be nice, and check out our Code of Conduct.












                      LiMi is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      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

                      Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                      Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                      A Topological Invariant for $pi_3(U(n))$