How to interact properly with four Eloquent models?

The project consists of four related tables:

1. orders (id, status, client_email, partner_id)
2. order_products (id, order_id, product_id, quantity, price)
3. partners (id, email, name)
4. prods* (id, name, price, vendor_id)
*the products table and have used for other function, so use here "prods"

create 4 models:
<?php
//App\Order.php
 namespace App;
 use Illuminate\Database\Eloquent\Model;

 class Order extends Model
{
 protected $fillable = [
 'id', 'status', 'client_email', 'partner_id',
];

public function partner()
{
 return $this->'s belongsto('App\Partner');
}

public function order_product()
{
//belongsToMany - returns server error
 return $this->'s belongsto('App\Prod', 'order_products');
}

public function prod()
{
 return $this->'s belongsto('App\Prod');
}
 }

<?php
//App\Partner.php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Partner extends Model
{
 protected $fillable = ['id', 'email', 'name'];

public function order()
{
 return $this->hasOne('App\Order');
}
}

<?php
//App\Order_product.php
 namespace App;
 use Illuminate\Database\Eloquent\Model;

 class Order_product extends Model
{
 protected $fillable = ['order_id', 'product_id', 'quantity', 'price'];

 public function order()
{
 return $this->hasOne('App\Order');
}
 }

<?php
//App\Prod.php
 namespace App;
 use Illuminate\Database\Eloquent\Model;

 class Prod extends Model
{
 protected $fillable = ['order_id', 'product_id', 'name', 'quantity', 'price'];

 public function order()
{
 //get all the names of products in order
//
 return $this->hasManyThrough('App\Order', 'App\Order_product');
}

 }


Next, create an Order Resource:
<?php
//App\Http\Resources\Order.php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;

class Order extends JsonResource
{
 public function toArray($request)
{
 return [
 'id' => $this->id,
 'status' => $this->status,
 'client_email' => $this->client_email,
 'partner_id' => $this->partner_id,
 'partner_name' => $this->partner->name,

 //returns server error
 'product_id' => Prod::collection($this->order_product), 
 'name' => $this->prod->name,

];
}
}

create Prod Resource:
<?php
//App/Http/Resources\Prode.php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;

class Prod extends JsonResource
{
 public function toArray($request)
{
 return [
 'id' => $this->id,
 'name' => $this->name,
 'price' => $this->price,
];
}
}

And return to the Order Controller:
<?php
//App\Http\Controllers\Api\OrderController.php
namespace App\Http\Controllers\Api;

use App\Order;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Resources\Order as OrderResource;

class OrderController extends Controller
{
public function index()
{
 $orders = Order::with(['partner', 'order_product', 'prod'])->get();
 return OrderResource::collection($orders);
}
}

How can I get the product name in the order?
April 7th 20 at 15:38
1 answer
April 7th 20 at 15:40
Add to the model Order hasManyThrough, through the order_products table. And then you will get a single query $order->products all products and then have a loop go through products and taking name
Corrected model Prod, and then not know what to do - Kory_Ryan commented on April 7th 20 at 15:43

Find more questions by tags Laravel