student and teacher and course relationships in laravel - php

I have a problem with the relationships in my project in Laravel:
My project consists of:
Student (ID- Name - Mobile - Email)
Teacher (ID - Name - Mobile - Email)
Course (ID - Name - description)
Relationships :
Each course is offered by one or more teachers.
Each teacher offers one or more courses.
Each student enrolled in one or more courses.
Each student attended one or more courses at one teacher
Clarification :
course & teacher = (Many To Many).
course & student = (Many To Many).
student & teacher = (Many To Many).
Through my search I found:
Next two images .. Are these relationships true?
My Work:
- migrations:
Schema::create('courses', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('name');
$table->text('description');
$table->timestamps();
});
Schema::create('students', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('name');
$table->string('mobile');
$table->string('email');
$table->timestamps();
});
Schema::create('teachers', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('name');
$table->string('mobile');
$table->string('email');
$table->timestamps();
});
Schema::create('enrollments', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('course_id')->unsigned();
$table->integer('student_id')->unsigned();
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
$table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
$table->timestamps();
});
Schema::create('teaches', function (Blueprint $table) {
$table->increments('id');
$table->integer('course_id')->unsigned();
$table->integer('teacher_id')->unsigned();
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
$table->foreign('teacher_id')->references('id')->on('teachers')->onDelete('cascade');
$table->timestamps();
});
Schema::create('student_course_teacher', function (Blueprint $table) {
$table->increments('id');
$table->integer('enroll_student_id')->unsigned();
$table->integer('enroll_course_id')->unsigned();
$table->integer('teach_teacher_id')->unsigned();
$table->foreign('enroll_student_id')->references('student_id')->on('enrollments')->onDelete('cascade');
$table->foreign('enroll_course_id')->references('course_id')->on('enrollments')->onDelete('cascade');
$table->foreign('teach_teacher_id')->references('teacher_id')->on('teaches')->onDelete('cascade');
$table->timestamps();
});
- models:
- Course:
class Course extends Model
{
protected $table = 'courses';
public function students()
{
return $this->belongsToMany(Student::class, 'enrollments', 'student_id', 'course_id')->withTimestamps();
}
public function teachers()
{
return $this->belongsToMany(Teacher::class, 'teaches', 'teacher_id', 'course_id')->withTimestamps();
}
}
- Student:
class Student extends Model
{
protected $table = 'students';
public function courses()
{
return $this->belongsToMany(Course::class, 'enrollments', 'course_id', 'student_id')->withTimestamps();
}
}
- Teacher:
class Teacher extends Model
{
protected $table = 'teachers';
public function courses()
{
return $this->belongsToMany(Course::class, 'teaches', 'course_id', 'teacher_id')->withTimestamps();
}
}
- Enrollment:
class Enrollment extends Model
{
protected $table = 'enrollments';
}
- Teach:
class Teach extends Model
{
protected $table = 'teaches';
}
Caution: I can not make relationships in (Enrollment & Teach).
My Requests:
Complete relationships
I want to get students for a certain course with teacher information.
I want to take courses with her students and teachers.
I want to get certain teacher courses with students.
Waiting for help,,

This is obviously not tested, but should help you figure out a solution towards your problem.
Name Of Tables
courses (For Course Model)
students (For Student Model)
teachers (For Teacher Model)
course_student (For Many To Many Relation between Student & Course) - For this you will create an extra model (CourseStudent), which will actually be used for Point 6.
course_teacher (For Many To Many Relation between Teacher & Course)
course_student_teacher (For Many To Many Relation between CourseStudent & Teacher)
Models
Course Model
class Course extends Model
{
public function students()
{
return $this->belongsToMany(Student::class)->withTimestamps();
}
public function teachers()
{
return $this->belongsToMany(Teacher::class)->withTimestamps();
}
}
Students Model
class Student extends Model
{
public function courses()
{
return $this->belongsToMany(Course::class)->withTimestamps();
}
}
Teachers Model
class Teacher extends Model
{
public function courses()
{
return $this->belongsToMany(Course::class)->withTimestamps();
}
// Courses/Students Currently Being Taught
public function courseStudents()
{
return $this->belongsToMany(CourseStudent::class, 'course_student_teacher')
->withTimestamps();
}
// List Of Students Currently Taught For Different Courses
public function allStudentsCurrentlyTaught()
{
return $this->courseStudents()->get()->map(function($courseStudent) {
return $courseStudent->student;
})->unique('id');
}
// List Of Courses Currently Taught To Different Students
public function coursesCurrentlyTaughtToStudents()
{
return $this->courseStudents()->get()->map(function($courseStudent) {
return $courseStudent->course;
})->unique('id');
}
// List Of Courses Taught To A Particular Students
public function coursesTaughtToAStudent($studentId)
{
return $this->courseStudents()->get()->where('student_id', $studentId)->map(function($courseStudent) {
return $courseStudent->course;
});
}
// List Of Students Taught For A Particular Course
public function coursesTaughtForACourse($courseId)
{
return $this->courseStudents()->get()->where('course_id', $courseId)->map(function($courseStudent) {
return $courseStudent->course;
});
}
}
CourseStudent Model for creating pivot table course_student_teacher
class CourseStudent extends Model
{
protected $table = 'course_student';
public function course()
{
return $this->belongsTo(Course::class)->withTimestamps();
}
public function student()
{
return $this->belongsTo(Student::class)->withTimestamps();
}
public function teachers()
{
return $this->belongsToMany(Teacher::class, 'course_student_teacher')->withTimestamps();
}
}

Related

BelongsTo in Laravel

I have a table which is named cars with 2 fields id, matriculation.
Then, I have another table which is named series with 2 fields id, name.
I have created my fk_serie on my table cars.
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('matriculation', 25);
$table->integer('fk_serie')->unsigned();
$table->foreign('fk_serie')->references('id_serie')->on('serie');
$table->timestamps();
});
}
Here is my information about the table series.
public function up()
{
Schema::create('series', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 30);
$table->timestamps();
});
}
In my model, I only have a function on the model Car.
public function serie(){
return $this->belongsTo('App\Serie', 'fk_serie');
}
I don't have nothing in my model Serie
class Serie extends Model
{
//
}
Is it normal that the model Serie is empty?
Because, my join works.
What do you think ?
Is there an error?
As Dparoli mentioned in comments if you don't need the below query then your above structure of relationship is normal
Serie::with('cars')->find($id)
But if you want to setup relationship in Serie Model you can do something like below:
class Serie extends Model
{
public function cars() {
return $this->hasMany('App\Car', 'fk_serie'); // your relationship
}
}
And after that you can do:
$series = Serie::with('cars')->find($id); //eager loading cars
$cars = $series->first()->cars;

How to create follower relationships using Eloquent and Laravel?

So, I'm trying to create a relationship where users can follow other users or follow categories.
My intuition says that what I've done so far is not the right way of doing things. I'm especially confounded by how to create the follower - followee relationship.
TABLES:
Users
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('password');
$table->string('first_name');
});
}
Categories
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('category');
});
}
Follows
public function up()
{
Schema::create('follows', function (Blueprint $table) {
$table->increments('id');
$table->integer('follower_id');
$table->integer('followee_id')->nullable();
$table->integer('category_id')->nullable();
});
}
MODELS:
User
class User extends Model implements Authenticatable
{
public function follows()
{
return $this->hasMany('App\Follow');
}
}
Category
class Category extends Model
{
public function follows()
{
return $this->hasMany('App\Follow');
}
}
Follow
class Follow extends Model
{
public function post()
{
return $this->belongsTo('App\User');
}
public function source()
{
return $this->belongsTo('App\Category');
}
}
According to your scenario, it is recommended that you use Polymorphic Many To Many relationship.
Schema:
users
id - integer
...
categories
id - integer
...
followables
user_id - integer
followable_id - integer
followable_type - string
Models:
User:
public function followers()
{
return $this->morphToMany(User::class, 'followables');
}
public function following()
{
return $this->morphedByMany(User::class, 'followables');
}
Category:
public function followers()
{
return $this->morphToMany(User::class, 'followables');
}
Then you can create the relationship like:
When following an User:
$user->followers()->create(['user_id' => 12])
When following a Category:
$category->followers()->create(['user_id' => 25])
Hope it helps.

How to define relation in laravel 5.3 eloquent model?

I have tables in my database schema as follows,
Is it pivot table?
How can I define relationship in eloquent model?
class Role extends Model {
public $timestamps = false;
public function permissions() {
return $this->hasMany('App\Models\RolePermission', 'permissions_id');
}
}
Is this correct way to define relationship? Please help me to understand.
and
class RolePermission extends Model {
public $timestamps = false;
public function role() {
return $this->belongsTo('App\Models\Role', 'roles_id');
}
}
The problem is that you need to name your table permission_role to follow the design pragma.
Role Schema
Schema::create('roles', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Permission schema
Schema::create('permissions', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Then you just need the permission_role table:
Permission_Role schema
Schema::create('permission_role', function(Blueprint $table){
$table->increments('id');
$table->integer('permission_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
Then you just set up your models like this:
class Role {
public function permissions() {
return $this->hasMany(App\Permission::class);
}
}
Then of course your permission class
class Permission {
public function role() {
return $this->belongsToMany(App\Role::class);
}
}
its a many to many relationship bond together by a pivot table Permission_Role. there for each table belongsToMany not hasOne or hasMany
class Role {
public function permissions() {
return $this->belongsToMany(App\Permission::class);
}
}
class Permission {
public function role() {
return $this->belongsToMany(App\Role::class);
}
}

Laravel 5 pivot table project<->user

I find the pivot tables pretty complicated and I don't see what to do next or what I am doing wrong, I've found some tutorials but didn't help me in my needs.
I have a projects and users with a many-to-many relation.
One project hasMany users and One user hasMany projects.
What I have now leaves projects without a relationship to a user.
This is what I have so far:
Projects table
class CreateProjectsTable extends Migration {
public function up()
{
Schema::create('projects', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->date('completion_date');
$table->integer('completed')->default(0);
$table->integer('active')->default(0);
$table->timestamps();
});
}
Users table
class CreateUsersTable extends Migration {
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->integer('company_id');
$table->integer('project_id');
$table->integer('usertype_id')->default(0);
$table->string('username');
$table->string('password');
});
}
Project User table (pivot)
class CreateProjectUsersTable extends Migration {
public function up()
{
Schema::create('project_users', function(Blueprint $table)
{
$table->increments('id');
$table->integer('project_id')->references('id')->on('project');;
$table->integer('user_id')->references('id')->on('user');;
});
}
User model
public function projects() {
return $this->belongsToMany('App\Project', 'project_users', 'user_id', 'project_id');
}
Project model
public function users() {
return $this->belongsToMany('App\User', 'project_users', 'project_id', 'user_id');
}
Project controller
public function index(Project $project)
{
$projects = $project->with('users')->get();
dd($projects);
$currenttime = Carbon::now();
//return view('project.index', array('projects' => $projects, 'currenttime' => $currenttime));
return view('user.index', compact('projects'));
}
The relationship in your User model is not correct. You have to swap the keys.
public function projects() {
return $this->belongsToMany('App\Project', 'project_users', 'user_id', 'project_id');
}
Edit regarding latest comment:
Don't think about the pivot table, as long as you have your relations setup correctly, which I believe they are, Laravel handles all of that for you.
Now $projects->users does not make any sense because projects does not have users. projects is just a collection of Project. Each Project within that collection will have a users relation. You would have to iterate through the collection to view each Project's users.
foreach($projects as $project) {
foreach($project->users as $user) {
echo $user;
}
}

Laravel 4 relation not working

I have the follow migration code (simplified):
Ads Table
class CreateAdsTable extends Migration {
public function up()
{
Schema::create('ads', function(Blueprint $table) {
$table->increments('id');
$table->integer('authors_id')->unsigned()->index();
$table->foreign('authors_id')->references('id')->on('authors');
$table->timestamps();
});
}
}
Authors Table
class CreateAuthorsTable extends Migration {
public function up()
{
Schema::create('authors', function(Blueprint $table) {
$table->increments('id');
$table->string('name', 200);
$table->timestamps();
});
}
}
And my models are:
Ad Model
class Ad extends \Eloquent {
protected $table = 'ads';
// Ad __hasOne__ Author
public function author() {
$this->belongsTo('Author');
}
}
Author Model
class Author extends \Eloquent {
// The database table used by the model
protected $table = 'authors';
// Author __hasMany__ Ads
public function ads() {
$this->hasMany('Ad');
}
}
But when I try to get the author using Ad::find(1)->author i receive Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation.
Someone can help me and find the error?
You have to return it:
return $this->belongsTo('Author');

Categories