laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.42k stars 10.99k forks source link

Eloquent relationship not working in another model #23564

Closed krnsptr closed 6 years ago

krnsptr commented 6 years ago

Description:

Eloquent relationship seems not working in another model when there is no relationship between the two models.

Example Code:

class Comment extends \Eloquent {
   protected $fillable = ['category_id'];   

   public function category() {
    return $this->belongsTo('App\Category', 'category_id', 'id');
  }
}

class Category extends \Eloquent {
    protected $table = 'categories';
}

class NotRelatedController extends Controller { 
   public function foo() {
    return Comment::find(123)->category_id; //returns appropriate ID 
  }

   public function bar($comment_id) {
    return Comment::find(123)->category->id; // returns appropriate ID 
  }

  public function debug() {
    return Comment::find(123)->category()->toSql();
    //result:
    //select * from `categories` where `categories`.`id` = ?
  }

}

class NotRelatedModel extends \Eloquent { 

   public function foo() {
    return Comment::find(123)->category_id; //returns appropriate ID 
  }

   public function bar() {
    return Comment::find(123)->category->id; //does NOT return appropriate ID 
  }

  public function debug() {
    return Comment::find(123)->category()->toSql();
    //result
    //select * from `categories`
  }
}
staudenmeir commented 6 years ago

That seems highly unlikely.

Can you add dd(Comment::find(123)->category()) to NotRelatedModel::debug() and post the result?

krnsptr commented 6 years ago

in NotRelatedController

BelongsTo {#426 ▼
  #child: ProgramStudi {#439 ▶}
  #foreignKey: "id_perguruan_tinggi"
  #ownerKey: "id"
  #relation: "perguruan_tinggi"
  #query: Builder {#434 ▼
    #query: Builder {#417 ▼
      +connection: MySqlConnection {#147 ▶}
      +grammar: MySqlGrammar {#148 ▶}
      +processor: MySqlProcessor {#149}
      +bindings: array:6 [▼
        "select" => []
        "join" => []
        "where" => array:1 [▼
          0 => "11"
        ]
        "having" => []
        "order" => []
        "union" => []
      ]
      +aggregate: null
      +columns: null
      +distinct: false
      +from: "perguruan_tinggi"
      +joins: null
      +wheres: array:1 [▼
        0 => array:5 [▼
          "type" => "Basic"
          "column" => "perguruan_tinggi.id"
          "operator" => "="
          "value" => "11"
          "boolean" => "and"
        ]
      ]
      +groups: null
      +havings: null
      +orders: null
      +limit: null
      +offset: null
      +unions: null
      +unionLimit: null
      +unionOffset: null
      +unionOrders: null
      +lock: null
      +operators: array:30 [▶]
      +useWritePdo: false
    }
    #model: PerguruanTinggi {#440 ▶}
    #eagerLoad: []
    #localMacros: []
    #onDelete: null
    #passthru: array:11 [▶]
    #scopes: []
    #removedScopes: []
  }
  #parent: ProgramStudi {#439 ▼
    #table: "program_studi"
    +timestamps: false
    #fillable: array:3 [▶]
    #appends: array:1 [▶]
    #hidden: array:3 [▶]
    #connection: "mysql"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:4 [▼
      "id" => "1100011"
      "nama" => "AGRIBISNIS"
      "jurusan" => "1"
      "id_perguruan_tinggi" => "11"
    ]
    #original: array:4 [▼
      "id" => "1100011"
      "nama" => "AGRIBISNIS"
      "jurusan" => "1"
      "id_perguruan_tinggi" => "11"
    ]
    #casts: []
    #dates: []
    #dateFormat: null
    #events: []
    #observables: []
    #relations: []
    #touches: []
    #visible: []
    #guarded: array:1 [▶]
  }
  #related: PerguruanTinggi {#440 ▼
    #table: "perguruan_tinggi"
    +timestamps: false
    #fillable: array:1 [▶]
    #appends: array:1 [▶]
    #hidden: array:1 [▶]
    #connection: "mysql"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: false
    +wasRecentlyCreated: false
    #attributes: []
    #original: []
    #casts: []
    #dates: []
    #dateFormat: null
    #events: []
    #observables: []
    #relations: []
    #touches: []
    #visible: []
    #guarded: array:1 [▼
      0 => "*"
    ]
  }
  #withDefault: null
}

in NotRelatedModel (note that the WHERE clause is missing):

BelongsTo {#439 ▼
  #child: ProgramStudi {#454 ▶}
  #foreignKey: "id_perguruan_tinggi"
  #ownerKey: "id"
  #relation: "perguruan_tinggi"
  #query: Builder {#453 ▼
    #query: Builder {#437 ▼
      +connection: MySqlConnection {#147 ▶}
      +grammar: MySqlGrammar {#148 ▶}
      +processor: MySqlProcessor {#149}
      +bindings: array:6 [▼
        "select" => []
        "join" => []
        "where" => []
        "having" => []
        "order" => []
        "union" => []
      ]
      +aggregate: null
      +columns: null
      +distinct: false
      +from: "perguruan_tinggi"
      +joins: null
      +wheres: []
      +groups: null
      +havings: null
      +orders: null
      +limit: null
      +offset: null
      +unions: null
      +unionLimit: null
      +unionOffset: null
      +unionOrders: null
      +lock: null
      +operators: array:30 [▶]
      +useWritePdo: false
    }
    #model: PerguruanTinggi {#436 ▶}
    #eagerLoad: []
    #localMacros: []
    #onDelete: null
    #passthru: array:11 [▶]
    #scopes: []
    #removedScopes: []
  }
  #parent: ProgramStudi {#454 ▼
    #table: "program_studi"
    +timestamps: false
    #fillable: array:3 [▶]
    #appends: array:1 [▶]
    #hidden: array:3 [▶]
    #connection: "mysql"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:4 [▼
      "id" => "1100011"
      "nama" => "AGRIBISNIS"
      "jurusan" => "1"
      "id_perguruan_tinggi" => "11"
    ]
    #original: array:4 [▼
      "id" => "1100011"
      "nama" => "AGRIBISNIS"
      "jurusan" => "1"
      "id_perguruan_tinggi" => "11"
    ]
    #casts: []
    #dates: []
    #dateFormat: null
    #events: []
    #observables: []
    #relations: []
    #touches: []
    #visible: []
    #guarded: array:1 [▶]
  }
  #related: PerguruanTinggi {#436 ▼
    #table: "perguruan_tinggi"
    +timestamps: false
    #fillable: array:1 [▶]
    #appends: array:1 [▶]
    #hidden: array:1 [▶]
    #connection: "mysql"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: false
    +wasRecentlyCreated: false
    #attributes: []
    #original: []
    #casts: []
    #dates: []
    #dateFormat: null
    #events: []
    #observables: []
    #relations: []
    #touches: []
    #visible: []
    #guarded: array:1 [▶]
  }
  #withDefault: null
}
staudenmeir commented 6 years ago

Do you have any scopes or other special features in your models?

Can you reproduce the problem using Category and Comment?

krnsptr commented 6 years ago

ah. sorry. I forgot how to reproduce it. a workaround came to my mind to avoid that problem, and my code base has been gone too far.

rajkananirk commented 2 years ago

See on this https://laratuto.com/laravel-eloquent-relations-not-working/

AntonOfTheWoods commented 10 months ago

@krnsptr Amazingly annoying... I have a very similar problem and I just wasted a minute or two because you created a ticket, wasted people's time and didn't bother to follow up.