0
votes

I am developing an application for the first time using Laravel. I need to fill a form using model-form binding which requires passing the data to the form.

Now the data which I need to pass is in a table called "teachers". It has corresponding Model and Controller both. The users table is the standard Laravel table. The users and teachers are related via "email". I tested the relashionships on Tinker which shows desired output as follows.

>>> $user = App\User::find(15);
=> App\User {#2947
id: 15,
name: "First",
email: "[email protected]",
email_verified_at: null,
created_at: "2019-04-19 17:10:59",
updated_at: "2019-04-19 17:10:59", }
>>> $user->teacher
=> App\teacher {#2948
sdrn: 856,
name: "First",
surname: null,
deptname: null,
deptcode: null,
designation: null,
email: "[email protected]",
mobile: null,
cc: "N",
created_at: "2019-04-19 17:10:59",
updated_at: "2019-04-19 17:10:59", }

Thus, the relationships are working alirght since the tinker commands are bringing correct row from teachers table.

However when I get the logged in teacher via Auth library as

 $user = Auth::user();
 $teacher = $user->teacher();
 dd();

the output of dd() shows something like

BelongsTo {#270 ▼
#child: User {#273 ▶}
#foreignKey: "email"
#ownerKey: "email"
#relationName: "teacher"
#query: Builder {#267 ▶}
#parent: User {#273 ▶}
#related: teacher {#262 ▶}
#withDefault: null
}

This is not what I need. I need to pass the corresponding row from teachers table to the form. Tinker gives the desired result alright but the Auth:user() is returning some funny object.

What am I missing?

2

2 Answers

1
votes

You can do either:

$user->teacher()->first()

to fetch the first (and only) result of the relationship, or a "magic" shortcut for that:

$user->teacher
0
votes

I hope this will help you

$user = Auth::user();
$teacher = $user->teacher;  //this will load related article models as a collection
dd($teacher);