ErrorException: Trying to get property user_id of non-object
up vote
-3
down vote
favorite
I am using $hidden
and $appends
to hide pivot
keyword from json array and pull the objects into main array. In the game model I added below script to do functionality as I said above
protected $hidden = ['pivot'];
protected $appends = ['user_id','highscore', 'level'];
public function getUserIdAttribute()
{
return $this->pivot->user_id;
}
public function getHighScoreAttribute()
{
return $this->pivot->highscore;
}
public function getLevelAttribute()
{
return $this->pivot->level;
}
Now the problem is that I want to fetch all games and I know it is so simple I just added into controller below script
$Games = Game::all();
return new GameResource($Games); /* GameResource is for API */
But the this one is return following error after adding above script into model.
ErrorException: Trying to get property user_id of non-object
Anyone can guide me please how to manage both of functionality because I need both of. I would appreciate if someone kindly guide.
After @emix Comment
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
php laravel laravel-5.6
add a comment |
up vote
-3
down vote
favorite
I am using $hidden
and $appends
to hide pivot
keyword from json array and pull the objects into main array. In the game model I added below script to do functionality as I said above
protected $hidden = ['pivot'];
protected $appends = ['user_id','highscore', 'level'];
public function getUserIdAttribute()
{
return $this->pivot->user_id;
}
public function getHighScoreAttribute()
{
return $this->pivot->highscore;
}
public function getLevelAttribute()
{
return $this->pivot->level;
}
Now the problem is that I want to fetch all games and I know it is so simple I just added into controller below script
$Games = Game::all();
return new GameResource($Games); /* GameResource is for API */
But the this one is return following error after adding above script into model.
ErrorException: Trying to get property user_id of non-object
Anyone can guide me please how to manage both of functionality because I need both of. I would appreciate if someone kindly guide.
After @emix Comment
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
php laravel laravel-5.6
Possible duplicate of Reference - What does this error mean in PHP?
– emix
Nov 7 at 12:23
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
– Script Lover
Nov 7 at 12:25
1
Well obviously yourGameResource
class tries to access thepivot
property which is null. Mentioned Wiki explains well how to handle such situations.
– emix
Nov 7 at 12:26
@emix above one is so useful can you kindly help me more please
– Script Lover
Nov 7 at 12:28
@ScriptLover can you post your Game model user relationship?
– adam
Nov 7 at 15:12
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I am using $hidden
and $appends
to hide pivot
keyword from json array and pull the objects into main array. In the game model I added below script to do functionality as I said above
protected $hidden = ['pivot'];
protected $appends = ['user_id','highscore', 'level'];
public function getUserIdAttribute()
{
return $this->pivot->user_id;
}
public function getHighScoreAttribute()
{
return $this->pivot->highscore;
}
public function getLevelAttribute()
{
return $this->pivot->level;
}
Now the problem is that I want to fetch all games and I know it is so simple I just added into controller below script
$Games = Game::all();
return new GameResource($Games); /* GameResource is for API */
But the this one is return following error after adding above script into model.
ErrorException: Trying to get property user_id of non-object
Anyone can guide me please how to manage both of functionality because I need both of. I would appreciate if someone kindly guide.
After @emix Comment
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
php laravel laravel-5.6
I am using $hidden
and $appends
to hide pivot
keyword from json array and pull the objects into main array. In the game model I added below script to do functionality as I said above
protected $hidden = ['pivot'];
protected $appends = ['user_id','highscore', 'level'];
public function getUserIdAttribute()
{
return $this->pivot->user_id;
}
public function getHighScoreAttribute()
{
return $this->pivot->highscore;
}
public function getLevelAttribute()
{
return $this->pivot->level;
}
Now the problem is that I want to fetch all games and I know it is so simple I just added into controller below script
$Games = Game::all();
return new GameResource($Games); /* GameResource is for API */
But the this one is return following error after adding above script into model.
ErrorException: Trying to get property user_id of non-object
Anyone can guide me please how to manage both of functionality because I need both of. I would appreciate if someone kindly guide.
After @emix Comment
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
php laravel laravel-5.6
php laravel laravel-5.6
edited Nov 7 at 12:26
asked Nov 7 at 12:22
Script Lover
226
226
Possible duplicate of Reference - What does this error mean in PHP?
– emix
Nov 7 at 12:23
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
– Script Lover
Nov 7 at 12:25
1
Well obviously yourGameResource
class tries to access thepivot
property which is null. Mentioned Wiki explains well how to handle such situations.
– emix
Nov 7 at 12:26
@emix above one is so useful can you kindly help me more please
– Script Lover
Nov 7 at 12:28
@ScriptLover can you post your Game model user relationship?
– adam
Nov 7 at 15:12
add a comment |
Possible duplicate of Reference - What does this error mean in PHP?
– emix
Nov 7 at 12:23
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
– Script Lover
Nov 7 at 12:25
1
Well obviously yourGameResource
class tries to access thepivot
property which is null. Mentioned Wiki explains well how to handle such situations.
– emix
Nov 7 at 12:26
@emix above one is so useful can you kindly help me more please
– Script Lover
Nov 7 at 12:28
@ScriptLover can you post your Game model user relationship?
– adam
Nov 7 at 15:12
Possible duplicate of Reference - What does this error mean in PHP?
– emix
Nov 7 at 12:23
Possible duplicate of Reference - What does this error mean in PHP?
– emix
Nov 7 at 12:23
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
– Script Lover
Nov 7 at 12:25
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
– Script Lover
Nov 7 at 12:25
1
1
Well obviously your
GameResource
class tries to access the pivot
property which is null. Mentioned Wiki explains well how to handle such situations.– emix
Nov 7 at 12:26
Well obviously your
GameResource
class tries to access the pivot
property which is null. Mentioned Wiki explains well how to handle such situations.– emix
Nov 7 at 12:26
@emix above one is so useful can you kindly help me more please
– Script Lover
Nov 7 at 12:28
@emix above one is so useful can you kindly help me more please
– Script Lover
Nov 7 at 12:28
@ScriptLover can you post your Game model user relationship?
– adam
Nov 7 at 15:12
@ScriptLover can you post your Game model user relationship?
– adam
Nov 7 at 15:12
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
The pivot is only available when you're fetching the models through the many-to-many relationship (BelongsToMany). The pivot represents the fields on the pivot table (eg. games_users) and thus fields that exist for that specific relationship.
When you're just pulling directly from the Game model (aka. games table), you have no relationship or pivot table to pull data from.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The pivot is only available when you're fetching the models through the many-to-many relationship (BelongsToMany). The pivot represents the fields on the pivot table (eg. games_users) and thus fields that exist for that specific relationship.
When you're just pulling directly from the Game model (aka. games table), you have no relationship or pivot table to pull data from.
add a comment |
up vote
0
down vote
The pivot is only available when you're fetching the models through the many-to-many relationship (BelongsToMany). The pivot represents the fields on the pivot table (eg. games_users) and thus fields that exist for that specific relationship.
When you're just pulling directly from the Game model (aka. games table), you have no relationship or pivot table to pull data from.
add a comment |
up vote
0
down vote
up vote
0
down vote
The pivot is only available when you're fetching the models through the many-to-many relationship (BelongsToMany). The pivot represents the fields on the pivot table (eg. games_users) and thus fields that exist for that specific relationship.
When you're just pulling directly from the Game model (aka. games table), you have no relationship or pivot table to pull data from.
The pivot is only available when you're fetching the models through the many-to-many relationship (BelongsToMany). The pivot represents the fields on the pivot table (eg. games_users) and thus fields that exist for that specific relationship.
When you're just pulling directly from the Game model (aka. games table), you have no relationship or pivot table to pull data from.
answered Nov 7 at 12:35
Devon
22k42645
22k42645
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189399%2ferrorexception-trying-to-get-property-user-id-of-non-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Reference - What does this error mean in PHP?
– emix
Nov 7 at 12:23
@emix it is not duplicated because the reference is about core php and I am using laravel. Off course laravel is built with php but still there is complicated structure so I think this one is not same as you refered
– Script Lover
Nov 7 at 12:25
1
Well obviously your
GameResource
class tries to access thepivot
property which is null. Mentioned Wiki explains well how to handle such situations.– emix
Nov 7 at 12:26
@emix above one is so useful can you kindly help me more please
– Script Lover
Nov 7 at 12:28
@ScriptLover can you post your Game model user relationship?
– adam
Nov 7 at 15:12