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');
}
}
php laravel-5
New contributor
add a comment |
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');
}
}
php laravel-5
New contributor
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 inPoint:: where
– Adam Hopkinson
9 hours ago
add a comment |
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');
}
}
php laravel-5
New contributor
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
php laravel-5
New contributor
New contributor
edited 8 hours ago
zedling
448220
448220
New contributor
asked 9 hours ago
LiMi
83
83
New contributor
New contributor
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 inPoint:: where
– Adam Hopkinson
9 hours ago
add a comment |
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 inPoint:: 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
add a comment |
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}}
add a comment |
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.
add a comment |
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}}
add a comment |
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}}
add a comment |
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}}
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}}
edited 8 hours ago
answered 8 hours ago
Saurabh Mistry
2,6691726
2,6691726
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 9 hours ago
Thomas Edwards
7,09031332
7,09031332
add a comment |
add a comment |
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.
LiMi is a new contributor. Be nice, and check out our Code of Conduct.
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%2f53371784%2fmanipulating-data-within-an-object%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
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