Call to undefined relationship in laravel - laravel

I have two tables departments and course, and I have defined a relationship between them but I received this error message:
Call to undefined relationship in laravel
Can anyone tell me please where's the problem?
Model Code:
public function course()
{
return $this->hasMany('App\Course','departments_id','id');
}
Model Department:
public function department()
{
return $this->belongsTo('App\Departments','departments_id');
}

So problem is so clear. Either in your Course model you have to rename the function department to departments, or to make a new one with the correct name.

First of all little correction in schema $table->integer('department_id'); should be $table->unsignedInteger('department_id');
And the problem is - in your migration column name is department_id but in your relation you have written departments_id there is a extra S!
just correct the column name from model.

in my case, i used:
::with('boards')
i just changed it to :
::with(['boards'])
with brackets
in my department Model:
public function boards()
{
return $this->hasMany(Board::class);
}

Related

hasOneThrough and hasManyThrough not working in Eloquent

I have the following tables
users userdatas workdays
--------- ----------- --------
id id id
user_id
workday_id
I would like to get a user's workdays (and later get all the users that have a specified workday, but one step at a time). I attempted several variations of this however it's not working. I believe the issue could be that workdays doesn't reference userdatas.
In the User model:
public function workday() {
return $this->hasOneThrough('App\Workday', 'App\Userdata');
}
I've also defined the following:
In Userdata
public function workday() {
return $this->hasOne('App\Workday');
}
In Workday
public function userdatas() {
return $this->belongsToMany('App\Userdata');
}
Does the reverse through relationship have to be defined in Workday? Any tips to get this to work?
Thank you!
You're following wrong relationship. As per your Database table design it should be Many to many relationship(belongsToMany).
In User Model pass the relationship as below.
public function workdays() {
return $this->belongsToMany('App\Workday', 'userdatas','user_id','workday_id');
}

Laravel Auth::user relationship

In Laravel, i´m trying to show relation elements between Auth::user (Users) and Departments. In User table i have id, name, and department_id. In Departments table I have id and name.
In user model i create
public function department()
{
return $this->belongsTo('App\Models\Department');
}
Then, in blade template I try
Auth::user()->department
But return null and doesn´t show linked departments. Null is incorrect, all users have departments. Soo, ¿Any help? ¿What´s wrong in relation?
You can try this User::with('departmento')->find(Auth::id());
This method uses less queries - the others go after the user table twice:
auth()->user()->load(['department']);
You should add 'department_id' as a second parameter ($foreignKey) when calling belongsTo method, because it will searching for departmento_id by default.
public function departamento()
{
return $this->belongsTo('App\Models\Departamento', 'department_id');
}
Or just rename User::departamento() method to User::department()
public function department()
{
return $this->belongsTo('App\Models\Departamento');
}
Relation works with Model. Auth uses the session it not uses relation
Use User Model instead
optional(User::find(Auth::id())->departmento)->department_name

Selecting specific column in ORM relationship Laravel 5.5

I have an order table with a primary key id and another table trips. I trip can have many orders. I have defined the relationship in this way
public function orders_currency() {
return $this->hasMany('App\Order', 'trip_id', 'id')->select('id', 'converted_currency_code');
}
Although I know that, I have to select id of the order table in order to get the result, that's why I am mentioning to select the id of the order table.
Despite this, I am getting the orders_currency index empty. Which angle am I missing in this case?
Any clue will be appreciated.
Did you set up the model correctly?
You'd do something like this in your model script I suppose
class Trips extends Model
{
public function orders() {
return $this->hasMany('Order');
}
I got the solution by doing this way
public function orders_currency() {
return $this->hasOne('App\Order', 'trip_id', 'id')
->select('trip_id','converted_currency_code');
}
I had to select referencing key in this case.

How do i access data using two BelongsTo?

I have three tables - "courses", "lessons" and "tasks". Each lesson belongsTo a course, and each task BelongsTo a lesson. I want to output a task, showing the task name, the lesson name, and the course name. How do I access the course table data? To get the lesson information linked to a course, I have used the following in my Task model:
$lessonName = $this->lessons->lesson_name;
To get the course name associated to that lesson, I have tried the following with no success, but I am really guessing here:
$courseName = $this->lessons->courses->course_name;
My model relationships are as follows:
Course.php
public function lessons()
{
return $this->hasMany('App\Lesson');
}
Lesson.php
public function tasks()
{
return $this->belongsTo('App\Task', 'task_id', 'id');
}
Task.php
public function lessons()
{
return $this->belongsTo('App\Lesson', 'lesson_id', 'id');
}
Where am I going wrong? Thanks
there is another way you can do this by using accessors.
on your Task model do the following:
public function getLessonAttribute(){
return Lesson::where('id', $this->attributes[*foreign_key_field*])->first();
}
Here you receive all the data regarding the lesson that the task belongs to, and can use them as any other attribute (field) of the model.
on your Lesson model get the course that it belongs to.
public function getCourseAttribute(){
return Course::where('id', $this->attributes[*course_foreign_key_field*])->first();
}
and then assuming that $task is your collection, you can access the lesson and the course like the following in blade:
$task->lesson->lesson_name and $task->lesson->course->course_name
In your lesson.php model doesn't exist relationship courses so there are your issue. Use answer what is told you #jeroenF
So you want the inverse of hasManyThrough?
The hasManyThrough feature of Laravel (see their site) facilitates connecting your Courses to Task directly, without having the intermediate connection in a separate relationship.
You are looking for the inverse?

Error in attaching/syncing many to many polymorphic relationship using Laravel

I have a Game class and a News class which can have many languages.
Here are my models:
Game:
id
string
News:
id
string
Languagable
id
languagable_id
languagable_type
Game:
public function languages()
{
return $this->morphToMany('Language', 'languagable');
}
News:
public function languages() {
return $this->morphToMany('Language', 'languagable');
}
Language:
public function games()
{
return $this->morphedByMany('Game', 'languagable');
}
public function news()
{
return $this->morphedByMany('News', 'languagable');
}
I followed the insertion or attaching/syncing that was instructed in this post:
How to save entries in many to many polymorphic relationship in Laravel?
When I did:
$game->languages()->attach(1);
This error shows up:
Column not found: 1054 Unknown column 'language_id' in 'field list' (SQL: insert into languagables (languagable_id, languagable_type, language_id) values (1, Game, 1))
It seems it's instead looking for another language_id instead of languagable_id. I've been at this for hours now and can't seem to get around this.
Any help would really be appreciated. Thanks!
Just needed to add a language_id since it's a many to many relationship.

Resources