Retrieving belongsTo relationship

Posted 10 months ago by bcharron

I am looking for some clarification on how to retrieve data from a belongsTo relationship. I have 2 models, Assignment and Category. An Assignment belongs to a Category and a Category hasMany Assignment. In my Assignment repository I am looking up assignments and their associated categories like so:

$assignment = $this->assignment
            ->with( ['category'] )
            ->findOrFail( $id )
            ;

This gets me the expected assignment object, and I see the category relationship object:

Assignment {#1724
  #softCascade: array:1 [
    0 => "workOrder"
  ]
  #fillable: array:130 [
    0 => "user_id"
    1 => "time_id"
    2 => "work_order"
    3 => "tag"
    4 => "company"
    5 => "identifier"
    6 => "date"
    7 => "time"
    8 => "time_slot_id"
    9 => "category"
    10 => "category_id"
    ...
  ]
  #hidden: []
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:134 [
    "id" => 430986
    "user_id" => 10006096
    "time_id" => null
    "work_order" => null
    "tag" => null
    "company" => null
    "identifier" => null
    "date" => "2017-12-21"
    "time" => "ALLDAY"
    "time_slot_id" => 50
    "category" => null
    "category_id" => 32
    ...
  ]
  #original: array:134 [
    "id" => 430986
    "user_id" => 10006096
    "time_id" => null
    "work_order" => null
    "tag" => null
    "company" => null
    "identifier" => null
    "date" => "2017-12-21"
    "time" => "ALLDAY"
    "time_slot_id" => 50
    "category" => null
    "category_id" => 32
    ...
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [
    "category" => Category {#1839
      #dates: array:1 [
        0 => "deleted_at"
      ]
      #primarykey: "id"
      #fillable: array:3 [
        0 => "name"
        1 => "time_off"
        2 => "points"
      ]
      #hidden: array:3 [
        0 => "deleted_at"
        1 => "created_at"
        2 => "updated_at"
      ]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [
        "id" => 32
        "name" => "Category Name"
        "time_off" => 0
        "points" => 0
        "deleted_at" => null
        "created_at" => "2017-12-20 17:52:40"
        "updated_at" => "2017-12-20 17:52:40"
      ]
      #original: array:7 [
        "id" => 32
        "name" => "Category Name"
        "time_off" => 0
        "points" => 0
        "deleted_at" => null
        "created_at" => "2017-12-20 17:52:40"
        "updated_at" => "2017-12-20 17:52:40"
      ]
      #changes: []
      #casts: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [
        0 => "*"
      ]
      #forceDeleting: false
    }
  ]
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [
    0 => "*"
  ]
  #forceDeleting: false
}

When I try to access the category with $assignment->category() it returns what looks like a query builder object for the belongs to relationship. In order to actually return the data I need to run $assignment->category()->get(). Is there a reason that the data is not available directly, instead of having to actually get() it?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.