dimanche 18 novembre 2018

Laravel: creating a view with data from different tables

I have 4 tables: Groups, Students, Subjects, Points. My database migrations: Subjects:

Schema::create('subjects', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();

Groups:

  Schema::create('groups', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->text('description');
        $table->timestamps();

Students:

 Schema::create('students', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('group_id')->nullable();
        $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
        $table->string('last_name');
        $table->string('given_name');
        $table->date('date_of_birth'); 
        $table->timestamps();

Points:

Schema::create('points', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('student_id');
$table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
$table->unsignedInteger('subject_id');
$table->foreign('subject_id')->references('id')->on('subjects')->onDelete('cascade');
$table->integer('points');
$table->timestamps();

My Eloquent models are:

Groups:

<?php
    namespace App\Models;
    use Illuminate\Database\Eloquent\Model;
    class Group extends Model
    {
        protected $fillable = ['name', 'description'];

        {
            return $this->hasMany('App/Models/Student');
        }   
    }

Points:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Point extends Model
{
    protected $fillable = ['points'];
    public function student()
    {
        return $this->belongsTo('App/Models/Student');
    }
      public function subject()
    {
        return $this->belongsTo('App/Models/Subject');
    }
}

Student:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
    protected $fillable = ['last_name', 'given_name', 'date_of_birth'];

    public function point()
    {
        return $this->hasMany('App/Models/Point');
    }
    public function group()
    {
        return $this->belongsTo('App/Models/Group');
    }
    }

Subject:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Subject extends Model
{
    protected $fillable = ['name'];
    public function points()
    {
        return $this->hasMany('App/Models/Point');
    }
}

I have already created resource controllers and views for each category. Now I need to created a view with groups, students, average points for each subject for each students and average points of the group for the subject. At this moment I have no idea how to do it, please point me into the right direction.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire