나는 여전히 라 라벨 관계가 어떻게 작동하는지 이해하지 못하고, 관계를 정의하기 위해 몇 시간 동안 노력했지만 그것을 할 수 없어서 도움을 주시면 감사하겠습니다.
내 엔티티는 Negotiation
및 Order
입니다. 주문에는 많은 협상이 있는데, 내가 제대로 정의하고 orders_negotiations
테이블이 제대로 공급되고 있습니다.
class Order extends Eloquent {
public function negotiations() {
return $this->belongsToMany('Negotiation', 'orders_negotiations');
}
}
그러나 이제 협상 측에서 주문을 검색하려고합니다. 나는 이것을 시도했다 :
class Negotiation extends Eloquent {
public function order() {
return $this->belongsTo('Order');
}
}
그러나 이것을 할 $negotiation->order->id
때이 오류가 발생합니다.Trying to get property of non-object
그리고 hasOne()
대신 시도 할 때 다음 오류가 발생합니다. SQLSTATE[42S22]: Column not found: 1054 Unknown column 'orders.negotiation_id' in 'where clause' (SQL: select * from
주문 where
주문 .
협상 _ID= 1 limit 1)
내가 뭘 잘못하고 있으며 어떻게해야합니까?
그래서 여기에 관계가 있습니다.
나는 Laravel이 관계를 지정하는 것을 더 이상 간단하게 만들 수 없다고 생각합니다. 왜냐하면 거의 정확하게 당신이 말하는 방식이기 때문입니다.
class Order extends Eloquent
{
public function negotiations()
{
return $this->hasMany('Negotiation');
}
}
class Negotiation extends Eloquent
{
public function order()
{
return $this->belongsTo('Order');
}
}
그리고 그게 다야. 그리고 물론 이러한 종류의 관계 에서는 테이블 / 마이그레이션에서 외래 키 를 지정해야 합니다.
Schema::create('orders', function(Blueprint $table)
{
$table->integer('id');
$table->string('number',64);
});
Schema::create('negotiations', function(Blueprint $table)
{
$table->integer('id');
$table->integer('order_id'); //here it is
$table->string('subject');
// ... and so on
});
그리고 그게 전부입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다