Laravel Eloquent Relationship has the same foreign key on the same table

cjdy13

I'm a beginner in Laravel and currently trying to create a ticketing system. My tickets table has two columns which both reference id from the users table namely user_id and handled_by. I am trying to eager load the full_name of handled_by but it is throwing an error "Trying to get property 'full_name' of non-object". I would like to know how to properly create an eloquent relationship between handled_by and tickets so I could display the handled_by's full_name. Below are snippets of my code.

User Model:

 public function tickets(){
        return $this->hasMany('\App\Ticket');
    }

 public function handledBy(){
      return $this->hasMany('\App\User', 'handled_by', 'id');
  } 

Ticket Model:

public function user(){
  return $this->belongsTo('\App\User');
}
public function handledBy(){
  return $this->belongsTo('\App\User', 'handled_by', 'id');
}

View:

@foreach($tickets as $ticket)
<td class="align-middle text-center">{{ $ticket->handled_by->full_name}}</td>
@endforeach

Controller:

$tickets = Ticket::all()->where('status_id', '1');
return view('admin.open_tickets', compact('tickets'));
TsaiKoga

In your User model, change \App\User to \App\Ticket, and change the method name from handlerBy to handlers:

public function handlers(){
      return $this->hasMany('\App\Ticket', 'handled_by', 'id');
} 

And call it by method's name instead of underline case name:

{{ $ticket->handledBy->full_name}}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Eloquent Foreign Key to Same Table

EF multiple foreign key relationship on same primary key ApplicationUser table

How to ensure many to many relationship has the same foreign key

Foreign key on same table

Relationships with same foreign key to same table Laravel 5

Eloquent relation setup, two foreign keys to same table laravel 5.2

Laravel Eloquent Relationship with different foreign key

MYSQL foreign key to the same table?

Using foreign key in the same table

Two columns in same table and same foreign key

Laravel have Two foreign Key from the same table

Foreign key same table laravel get tree view

Laravel migration: adding foreign key to the same table where ID is a string

Laravel: same table relationship error

SQL relationship to enforce rule that a table can only reference rows of another table with the same foreign key

Could a foreign key be a candidate key in the same table?

CRUD operations on a table with foreign key on same table

Entity Framework Table Splitting: not in the same type hierarchy / do not have a valid one to one foreign key relationship

tsql - How to copy data in the same table with new ID and with foreign key relationship

Eloquent Relationship on the same model

two foreign keys of same table (match between two profiles) in eloquent laravel

EF Code First Foreign Key Same Table

Multiple Foreign Key from the same table

Django multiple foreign key to a same table

Django model foreign key in the same table

MySQL two foreign key from same table

How to create a foreign key on the same table with Sequelize?

foreign key constraint is incorrectly formed [same table]

Oracle SQL foreign key from the same table