Reputation: 67
In a Laravel 5 project I am trying to display to a logged in user the details of their lesson packages. I am trying to use a where clause that gets the desired info. 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)
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:
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); 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.
Upvotes: 1
Views: 1700
Reputation: 4435
Since get()
method returns the array of collection irrespective of number of results. If you are expecting only single result you can use first()
method.
$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.
Upvotes: 1
Reputation: 163768
You need to get an ID from user object, so change you query to:
$lessonhours = Lessonhours::whereHas('players', function($q) {
$q->where('users_id', Auth::user()->id);
})->with('players')->get();
Upvotes: 1