Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 雄辩的关系:cloumn不';不存在_Php_Laravel - Fatal编程技术网

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