hf_vrp
hf_vrp

Reputation: 3

Laravel eloquent relations not working completely

I've a payment gateway, I want to access the product items after returning, so I created several tables Payments, Orders, OrderItems and configured the relationships between them. Each Payment is for one Order and each Order contains several OrderItem and each OrderItem contains one product. Now I want to access each Product through an object of the Payment Model, which returns the following error.

Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'orders.order_item_id' in 'field list' (SQL: select products.*, orders.order_item_id as laravel_through_key from products inner join orders on orders.id = products.order_id where orders.order_item_id = 68 limit 1)

$product_order_item = $payment->order->order_items->map(function ($order_item) {
            dd($order_item->product);
        });

Payment Model:

protected $table = "payments";
protected $guarded = [];

public function order() {
    return $this->belongsTo(Order::class);
}

Order Model:

protected $table = "orders";
protected $guarded = [];

public function payment() {
    return $this->hasOne(Payment::class);
}

public function order_items() {
    return $this->hasMany(OrderItem::class);
}

OrderItem Model:

protected $table = "order_items";
protected $guarded = [];

public function order() {
    return $this->belongsTo(Order::class);
}

public function product() {
    return $this->hasOneThrough(Product::class, Order::class);
}

Product Model:

protected $table = "products";
protected $guarded = [];

public function order_items() {
    return $this->belongsToMany(OrderItem::class);
}

That is migrations:

Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE');
            $table->unsignedInteger('amount');
            $table->unsignedInteger('res_code');
            $table->enum('status', ['paid', 'unpaid']);
            $table->timestamps();
        });

=====================================================

Schema::create('order_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('order_id');
            $table->foreign('order_id')->references('id')->on('orders')->onDelete('CASCADE');

            $table->unsignedBigInteger('product_id');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('CASCADE');

            $table->unsignedInteger('price');
            $table->timestamps();
        });

===========================================================

Schema::create('payments', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('order_id');
            $table->foreign('order_id')->references('id')->on('orders')->onDelete('CASCADE');
            $table->enum('status', ['paid', 'unpaid']);
            $table->unsignedInteger('ref_id');
            $table->unsignedInteger('res_id');
            $table->enum('gateway', ['idpay', 'zarinpal']);
            $table->timestamps();
        });

what is the problem? Thanks in advance...

Upvotes: 0

Views: 197

Answers (1)

Wilson
Wilson

Reputation: 825

You should use belongsTo for the product relationship of OrderItem Model

OrderItem Model:

public function product() {
    return $this->belongsTo(Product::class);
}

https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse


And hasMany for the order_items relationship of Product Model

Product Model:

public function order_items() {
    return $this->hasMany(OrderItem::class);
}

https://laravel.com/docs/8.x/eloquent-relationships#one-to-many

Upvotes: 0

Related Questions