skip to Main Content

Hello i have a table called order_product that i want to get values from it and the model for that table called order_product with values:

public $timestamps = false;

    protected $fillable = [
        'order_id',
        'product_id',
        'amount',
    ];

This is the code of the model Order :

public $timestamps = true;

    protected $fillable = [
        'order_number',
        'client_id',
        'description',
    ];


    public function client()
    {
        return $this->belongsTo(Client::class);
    }

    public function products()
    {
        return $this->belongsToMany(Product::class);
    }

    public function orders()
    {
        return $this->belongsToMany(order_product::class);
    }

A professional guy helped me and explained to me how the relation worked so the client and products work very good but the orders makes error in the sql.

This is the code im executing in the controller:

$orders = Order::where('id', $id)->firstOrFail();
$orders->load('client', 'products','orders');

The error that i get is:

SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘user_project_db.order_products’ doesn’t exist

What should be the name of the file order_product so the query can execute properly?

2

Answers


  1. protected $table = 'order_products; in the model will tell Laravel that the Order model’s data is stored in a table by that name.

    However, typically you’d have an Order model, a Products model, and a pivot table (potentially with a pivot model, if you need it) titled order_products. https://laravel.com/docs/9.x/eloquent-relationships#defining-custom-intermediate-table-models

    Login or Signup to reply.
  2. I change my answer after reading your answers below.

    Your table relationship is ordersorder_productproducts.

    https://webdevetc.com/blog/laravel-naming-conventions/
    under Pivot tables

    The way you named your pivot table is already correct.

    order_product is to connect orders to products in a many-to-many.

    So i think you can try to do the following.

    Inside model Product add this relationship.

    public function orders() 
    {
       return $this->belongsToMany(Order::class, 'order_product');
    }
    

    And in model Order add the other connection

    public function products() 
    {
       return $this->belongsToMany(Product::class, 'order_product');
    }
    

    belongsToMany accepts 2 parameter, 1st is model destination, and 2nd is table pivot name, in your case order_product.

    With this , an extra model OrderProduct is optional.

    To add a product into order , you can use attach

     $order = Order::find($order_id);
     $order->products()->attach($product_id);
    

    Or if you have extra fields within pivot table

    // first implement this change inside Order model
    
    return $this->belongsToMany(Product::class, 'order_product')
       ->withPivot('price', 'qty');
    
    // to attach for example price and qty
    $order->products()->attach($product_id, ['price' => 200', 'qty'=> 1]);
    

    To query the price

    $order_product = $order->products()
       ->where('product_id', $product_id)
       ->first()->pivot;
    $price = $order_product->price;
    $qty = $order_product->qty;
    

    And back to your own query.

    No need to add orders() inside Order model.

    And load only the first 2 relationship should be enough.
    $order->load('clients', 'products');

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search