Php 雄辩的关系:cloumn不';不存在
我是拉威尔的新手,我很难理解人际关系是如何运作的。我正在构建一个简单的电子商务应用程序,其中每个用户都有一些订单,订单有一个或多个子订单,每个子订单只链接到一个项目(请不要对我的方案发表评论;现在我只需要找出有说服力的,稍后将进行重构:) 以下是我的模型:Php 雄辩的关系:cloumn不';不存在,php,laravel,Php,Laravel,我是拉威尔的新手,我很难理解人际关系是如何运作的。我正在构建一个简单的电子商务应用程序,其中每个用户都有一些订单,订单有一个或多个子订单,每个子订单只链接到一个项目(请不要对我的方案发表评论;现在我只需要找出有说服力的,稍后将进行重构:) 以下是我的模型: class Order extends Model { //timestamp protected $created_at; public function sub_orders() { return
class Order extends Model
{
//timestamp
protected $created_at;
public function sub_orders() {
return $this->hasMany('App\SubOrder');
}
public function user() {
return $this->belongsTo('App\User');
}
}
class SubOrder extends Model
{
protected $fillable = array('delivery_date', 'quantity', 'total_price', 'delivery_status');
public function item() {
return $this->hasOne('App\Item');
}
public function order() {
return $this->belongsTo('App\Order');
}
}
class Item extends Model
{
//note - slug is kind of categorization and is common to many items
protected $fillable = array('sku', 'name', 'slug', 'unit_price');
}
以下是迁移:
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->timestamp('created_at');
//foreign keys
$table->unsignedInteger('user_id')->after('id');
$table->foreign('user_id')->references('id')->on('users') ->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}
class CreateSubOrdersTable extends Migration
{
public function up()
{
Schema::create('sub_orders', function (Blueprint $table) {
$table->increments('id');
$table->date('delivery_date');
$table->decimal('quantity', 5, 2);
$table->decimal('total_price', 7, 2);
$table->enum('delivery_status', ['pending_from_farmer', 'ready_for_customer', 'out_for_delivery', 'delivered']);
//foreign keys
$table->unsignedInteger('order_id')->after('id');
$table->foreign('order_id')->references('id')->on('orders') ->onDelete('cascade');
$table->unsignedInteger('item_id')->after('order_id');
$table->foreign('item_id')->references('id')->on('items') ->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('sub_orders');
}
}
class CreateItemsTable extends Migration
{
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->increments('id');
$table->string('sku')->unique();
$table->string('name');
$table->string('slug');
$table->decimal('unit_price', 5, 2);
});
}
public function down()
{
Schema::dropIfExists('items');
}
}
有问题的表达式是为什么我在我的web.php
中编写App\Order::all()[0]->sub_orders[0]->item
,并得到以下错误:
SQLSTATE[42703]: Undefined column: 7 ERROR: column items.sub_order_id does not exist
LINE 1: select * from "items" where "items"."sub_order_id" = $1 and ...
^ (SQL: select * from "items" where "items"."sub_order_id" = 1 and "items"."sub_order_id" is not null limit 1)
我不明白为什么它在
项目
表中查找子订单id
。正确的方法是什么?总体而言:使用hasOne
或belongsTo
定义1对1关系将影响Laravel查找外键的目标表hasOne
假设目标表中有一个my\u model\u id
。并且belongsTo
假设我的表中有一个target\u model\u id
class SubOrder extends Model
{
public function item() {
return $this->hasOne('App\Item', 'id', 'item_id');
}
}
或
class SubOrder extends Model
{
public function item() {
return $this-> belongsTo('App\Item');
}
}
据
Eloquent根据模型名称确定关系的外键。在上述情况下,手机型号被自动假定具有用户id外键。如果希望重写此约定,可以将第二个参数传递给hasOne方法:
或者定义关系的倒数
class Phone extends Model
{
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
在上面的示例中,Eloquent将尝试将手机型号上的用户id与用户型号上的id进行匹配
总体:使用
hasOne
或belongsTo
定义1对1关系将影响Laravel查找外键的目标表hasOne
假设目标表中有一个my\u model\u id
。并且belongsTo
假设我的表中有一个target\u model\u id
class SubOrder extends Model
{
public function item() {
return $this->hasOne('App\Item', 'id', 'item_id');
}
}
或
class SubOrder extends Model
{
public function item() {
return $this-> belongsTo('App\Item');
}
}
据
Eloquent根据模型名称确定关系的外键。在上述情况下,手机型号被自动假定具有用户id外键。如果希望重写此约定,可以将第二个参数传递给hasOne方法:
或者定义关系的倒数
class Phone extends Model
{
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
在上面的示例中,Eloquent将尝试将手机型号上的用户id与用户型号上的id进行匹配
您的
子订单
项目与项目具有类型为OneToOne
(hasOne
是双向的)的关系。因此,
elount
希望在项目
表中有子订单id
因此,解决方案是在
项目
模型中定义此关系的倒数(belongsTo
),您的子订单
项目与项目之间的关系类型为OneToOne
(hasOne
是双向的)。因此,
elount
希望在项目
表中有子订单id
因此,解决方案是在项
模型中定义此关系的倒数(belongsTo
)