Laravel 4 relation not working - php

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

Related

Laravel 7 One To Many Relations?

Below are all of the models, migrations and controller.
Donation Model
class Donation extends Model
{
protected $guarded =[];
public function users(){
return $this->hasMany(User::class);
}
public function items(){
return $this->belongsTo(DonationItems::class);
}
}
Donation Items Model:
class DonationItems extends Model
{
protected $guarded=[];
public function donation(){
return $this->hasMany(Donaition::class);
}
}
Donation Items Migration:
public function up()
{
Schema::create('donation_items', function (Blueprint $table) {
$table->id();
$table->string('category');
$table->timestamps();
});
}
Donation Migration:
public function up()
{
Schema::create('donations', function (Blueprint $table) {
$table->id();
$table->string('item');
$table->unsignedInteger('user_id');
$table->unsignedInteger('donation_item_id');
$table->timestamps();
});
}
In my controller I want to access the items as follows:
$don = Donation::all();
$don->items;
But I'm unable to achieve this.
Its not working because laravel follows one rule for relationships:
Remember, Eloquent will automatically determine the proper foreign key column on the Comment model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with _id. So, for this example, Eloquent will assume the foreign key on the Comment model is post_id.
So you can try by supplying local and foreign id
So it would look something like this
Donation Model
class Donation extends Model
{
protected $guarded =[];
public function users(){
return $this->hasMany(User::class);
}
public function items(){
return $this->belongsTo(DonationItems::class, 'donation_item_id', 'id');
}
}
Donation Items Model:
class DonationItems extends Model
{
protected $guarded=[];
public function donation(){
return $this->hasMany(DonationItems::class, 'id', 'donation_item_id');
}
}
I am writing from my head you might need to swap local and foreign ID's

set where for foriegn table in laravel relation

I have a table has many relation to other tables but it seperated with entity value please look at this :
i have this schema
public function up()
{
Schema::create('cards', function(Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('user_id')->unsigned()->nullable();
$table->integer('entity_id');
$table->string('entity');
$table->integer('qty')->nullable()->default('1');
});
}
public function up()
{
Schema::create('tickets', function(Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('title');
$table->string('summary');
$table->integer('amount');
$table->integer('stock')->default('0');
$table->integer('discount')->default('0');
});
}
public function up()
{
Schema::create('products', function(Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('title');
$table->integer('amount');
$table->integer('discount');
$table->text('description');
$table->integer('stock')->default('0');
});
}
and this relation in models
class Card extends Model
{
protected $table = 'cards';
public $timestamps = true;
public function ticket()
{
return $this->belongsTo('App\Models\Ticket', 'entity_id');
}
public function product()
{
return $this->belongsTo('App\Models\Product', 'entity_id');
}
}
i need to set where entity = 'ticket' before use belongsTo i mean is a table hase relation to many table base entity_id and i seperated it by entity column and base same vlue most have realation just.
You can do simply in your eloquent model file. do like this :
public function ticketWithCondition()
{
return $this->belongsTo('App\Models\Ticket', 'entity_id')->where('entity' , 'ticket');
}
public function ticket()
{
return $this->belongsTo('App\Models\Ticket', 'entity_id');
}
call like this :
// for show Card with EntityCondition
$comments = Card::find(123)->with('ticketWithCondition');
// for show comments without EntityCondition
$comments = Card::find(123)->with('ticket');

student and teacher and course relationships in laravel

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();
}
}

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

Categories