Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

What you mean by 'Im not able...' ?

Last updated 2 years ago.
0

jarektkaczyk said:

What you mean by 'Im not able...' ?

Im getting 'Trying to get property of non-object' when I do something like:

$user->notes[0]->creator->id

When I dump $user->notes[0] this error is logical.

dd($user->notes[0]);

object(Sportbundels\Note)#702 (20) {
  ["timestamps"]=>
  bool(false)
  ["table":protected]=>
  string(5) "notes"
  ["connection":protected]=>
  NULL
  ["primaryKey":protected]=>
  string(2) "id"
  ["perPage":protected]=>
  int(15)
  ["incrementing"]=>
  bool(true)
  ["attributes":protected]=>
  array(7) {
    ["id"]=>
    int(2)
    ["user_id"]=>
    int(2)
    ["creator"]=>
    int(3)
    ["text"]=>
    string(27) "Stephan is de nieuwe Zlatan"
    ["created_at"]=>
    string(19) "2014-03-26 12:25:53"
    ["updated_at"]=>
    string(19) "0000-00-00 00:00:00"
    ["deleted_at"]=>
    string(19) "0000-00-00 00:00:00"
  }
  ["original":protected]=>
  array(7) {
    ["id"]=>
    int(2)
    ["user_id"]=>
    int(2)
    ["creator"]=>
    int(3)
    ["text"]=>
    string(27) "Stephan is de nieuwe Zlatan"
    ["created_at"]=>
    string(19) "2014-03-26 12:25:53"
    ["updated_at"]=>
    string(19) "0000-00-00 00:00:00"
    ["deleted_at"]=>
    string(19) "0000-00-00 00:00:00"
  }
  ["relations":protected]=>
  array(1) {
    ["creator"]=>
    object(Sportbundels\User)#705 (20) {
      ["softDelete":protected]=>
      bool(true)
      ["table":protected]=>
      string(5) "users"
      ["connection":protected]=>
      string(7) "clubsql"
      ["primaryKey":protected]=>
      string(2) "id"
      ["perPage":protected]=>
      int(15)
      ["incrementing"]=>
      bool(true)
      ["timestamps"]=>
      bool(true)
      ["attributes":protected]=>
      array(46) {
        ["id"]=>
        int(3)
        ["account_id"]=>
        int(0)
        ["initials"]=>
        string(1) "R"
        ["firstname"]=>
        string(3) "Rob"
        ["insertion"]=>
        string(0) ""
        ["lastname"]=>
        string(6) "Troost"
        ["created_at"]=>
        string(19) "2014-03-26 10:28:25"
        ["updated_at"]=>
        string(19) "2014-04-01 20:14:07"
        ["deleted_at"]=>
        NULL
      }
      ["relations":protected]=>
      array(0) {
      }
      ["hidden":protected]=>
      array(0) {
      }
      ["visible":protected]=>
      array(0) {
      }
      ["appends":protected]=>
      array(0) {
      }
      ["fillable":protected]=>
      array(0) {
      }
      ["guarded":protected]=>
      array(1) {
        [0]=>
        string(1) "*"
      }
      ["dates":protected]=>
      array(0) {
      }
      ["touches":protected]=>
      array(0) {
      }
      ["observables":protected]=>
      array(0) {
      }
      ["with":protected]=>
      array(0) {
      }
      ["exists"]=>
      bool(true)
    }
  }
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["appends":protected]=>
  array(0) {
  }
  ["fillable":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["dates":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["with":protected]=>
  array(0) {
  }
  ["exists"]=>
  bool(true)
  ["softDelete":protected]=>
  bool(false)
}

The creator object is inside the relations array, why is that? I should be inside the creator right?

Last updated 2 years ago.
0

Looks good, show the models' relations and the code where you want to retrieve the creator.

Last updated 2 years ago.
0

I found the problem.

I named the column for the creator: 'creator' instead of 'creator_id'. The function name to get the creator by note was also creator. This was causing the problem.

Thnx for the help anywayz!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.