0

I am trying to display all the records from my orderItems table but it is displaying data from my products table. How would I switch this round? Have I done my relationships backwards?

desired results Display all order items from OrderItems table

**Product Model **

    <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\OrderItem;

class Product extends Model
{

    use HasFactory;
    protected $fillable = [
        'item_name', 'size', 'toppings','price',
    ];

    public function orderItem() {
        return $this->belongsTo(OrderItem::class,);

    }



}

**OrderItem Model **

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Product;

class OrderItem extends Model
{

    use HasFactory;
    protected $fillable = ['product_id'];


    public function products_rel(){
      return $this-> hasMany(Product::class );

    }
    public function Order(){
    return $this->hasMany(Order::Class);
}
}

orderItem Table

    public function up()
{
    Schema::create('order_items', function (Blueprint $table) {

        $table->id();
        $table->foreignId('product_id');

        $table->timestamps();
    });
}

Products Table

    public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->timestamps();
        $table->foreignId('order_item_id');

        $table->string('name');
        $table->string('size');
        $table->float('price');
        $table->foreignId('topping_id')->nullable();



    });
}

orderItem Controller

 <?php

namespace App\Http\Controllers;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use Illuminate\Http\Request;

class OrderItemsController extends Controller
{

    public function index()
    {


   $orderItems = OrderItem::all();
    return view('basket', ['orderItems'=> $orderItems]);
    }

    public function  create()
    {
        return view('welcome');
    }

    public function store ()
    {
    request()->validate([
        'product_id'=> 'required',

    ]);




        OrderItem::create([

            'product_id' =>request('product_id'),
        ]);

    }

}

output html

<table id="checkout">
    <tr>
    <th>Product</th>
    <th>Size</th>

    <th>Price</th>
    </tr>
    @foreach($orderItems as $orderItem)
        @foreach($orderItem->products_rel as $related_product)
            <tr>
            <td>{{$related_product->name}}</td>
            <td>{{$related_product->size}}</td>

            <td>{{$related_product->price}}</td>
        </tr>


        @endforeach
    @endforeach

</table>
1
  • It seems more likely that an order item would have one associated product (belongsTo), and a product would be associated with many order items (hasMany). Commented Jun 2, 2021 at 16:48

1 Answer 1

2

desired results Display all order items from OrderItems table

This order_items table contains products, so it is normal to show products there !!!

I think your problem is that you are getting all the items in your order_item table with this $orderItems = OrderItem::all(); while you want the orders, so you should query on orders table.

    $orders = Order::all();

also you are showing the products your self in foreach($orderItem->products_rel as $related_product)

@foreach($orderItems as $orderItem)
        @foreach($orderItem->products_rel as $related_product)
            <tr>
            <td>{{$related_product->name}}</td>
            <td>{{$related_product->size}}</td>

            <td>{{$related_product->price}}</td>
        </tr>


        @endforeach
    @endforeach

so if you dont want the products delete the second foreach

Sign up to request clarification or add additional context in comments.

1 Comment

The issue is its not displaying all the products

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.