[英]laravel 5 show related data to logged in user
In a Laravel 5 project I am trying to display to a logged in user the details of their lesson packages. 在Laravel 5项目中,我试图向登录的用户显示其课程包的详细信息。 I am trying to use a where clause that gets the desired info.
我正在尝试使用where子句来获取所需的信息。 It seems that I can't get relationship data from the collection that I am getting back.
看来我无法从要返回的集合中获取关系数据。 I am lost and am requesting some guidance.
我迷路了,需要一些指导。 I can't seem to find any info pertaining to this kind of problem.
我似乎找不到与此类问题有关的任何信息。
The current error: Undefined property: Illuminate\\Database\\Eloquent\\Collection::$players (View: /home/ubuntu/workspace/resources/views/auth/account/mylessonhours.blade.php) 当前错误:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ players(查看:/home/ubuntu/workspace/resources/views/auth/account/mylessonhours.blade.php)
My Controller: 我的控制器:
public function getMyLessonhours(Lessonhours $lessonhours, Packages $packages)
{
$packages = Packages::all();
$lessonhours = Lessonhours::whereHas('players', function($q)
{
$q->where('users_id', '=', Auth::user());
})->with('players')->get();
return view('auth.account.mylessonhours', compact('lessonhours'), compact('packages'));
}
My Relationships: Users: 我的关系:用户:
public function players()
{
return $this->hasMany('App\Players', 'users_id');
}
Players: 玩家:
public function users()
{
return $this->belongsTo('App\User', 'users_id');
}
Lessonhours: Lessonhours:
public function players()
{
return $this->belongsTo('App\Players', 'players_id');
}
public function packages()
{
return $this->belongsTo('App\Packages', 'packages_id');
}
public function hoursused()
{
return $this->hasMany('App\Hoursused', 'lessonhours_id');
}
And the part of the view that fails 而视图失败的部分
<h3>{{ $lessonhours->players->getFullName($lessonhours->players_id) }}</h3>
{{ $lessonhours->players->gender }}<br>
<a href="#" class="btn btn-default btn- pull-right">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
Edit {{ $lessonhours->players->getFullName($lessonhours->players->id) }}</a>
Birthdate: {{ $lessonhours->players->birthdate->format('m-d-Y') }}<br>
Family: {{ $lessonhours->players->users->famname }}<br>
<hr>
Thank you for help. 谢谢你的帮助。
UDATE: After doing a dd($lessonhours); UDATE:经过dd($ lessonhours); I got the following data in the collection which does show the desired info, I just continue to get the 'undefined' error above:
我在集合中得到了以下数据,该数据确实显示了所需的信息,我只是继续收到上面的“未定义”错误:
Collection {#216 ▼
#items: array:1 [▼
0 => Lessonhours {#202 ▼
#fillable: array:3 [▶]
+table: "lessonhours"
#dates: array:1 [▶]
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:6 [▼
"id" => 2
"players_id" => 2
"packages_id" => 2
"signup_date" => "2016-06-01"
"created_at" => "2016-11-19 03:31:11"
"updated_at" => "2016-11-19 03:31:11"
]
#original: array:6 [▶]
#relations: array:1 [▼
"players" => Players {#220 ▼
+table: "players"
#fillable: array:4 [▶]
#dates: array:1 [▶]
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:8 [▼
"id" => 2
"users_id" => 2
"fname" => "Girltest"
"lname" => "Testfam"
"gender" => "Female"
"birthdate" => "2010-10-04"
"created_at" => "2016-11-19 03:30:56"
"updated_at" => "2016-11-19 03:30:56"
]
#original: array:8 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
UPDATE: The error occurs with the following line: 更新:错误发生在下面的行:
<h3>{{ $lessonhours->players->getFullName($lessonhours->players_id) }}</h3>
The code works, but I only want one parent record. 该代码有效,但我只想要一个父记录。
You need to get an ID from user object, so change you query to: 您需要从用户对象获取ID,因此将查询更改为:
$lessonhours = Lessonhours::whereHas('players', function($q) {
$q->where('users_id', Auth::user()->id);
})->with('players')->get();
Since get()
method returns the array of collection irrespective of number of results. 由于
get()
方法返回集合数组,而与结果数无关。 If you are expecting only single result you can use first()
method. 如果只期望单个结果,则可以使用
first()
方法。
$lessonhours = Lessonhours::whereHas('players', function($q)
{
$q->where('users_id', '=', Auth::user()->id);
})->with('players')->first();
Or use for each
loop while rendering in the view. 或在视图中渲染时
for each
循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.