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










share|improve this question
























  • 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 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










  • @ScriptLover can you post your Game model user relationship?
    – adam
    Nov 7 at 15:12















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










share|improve this question
























  • 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 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










  • @ScriptLover can you post your Game model user relationship?
    – adam
    Nov 7 at 15:12













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • @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










  • @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 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










  • @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












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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 12:35









        Devon

        22k42645




        22k42645






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud