У меня есть таблица заказов, в которой столбцы customer_id и seller_id.Идентификатор_покупателя - это текущий пользователь, который покупает что-то у продавца.То, что я хочу, - это когда покупатель отправляет заказ, также должен присутствовать продавец_ид товара.
Вот как я создаю заказ в контроллере проверки внутри функции магазина
//Insert into the orders table
$order = Order::create([
'buyer_id' => auth()->user() ? auth()->user()->id : null,
'seller_id' => //what should i put here to query the owner of the product(the user who listed the product)
'shipping_email' => $request->email,
'shipping_name' => $request->name,
'shipping_city' => $request->city,
'shipping_phone' => $request->phone,
// 'error' => null,
]);
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'Seller'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
//public function isSeller() {
// return $this->seller;
//}
public function products()
{
return $this->hasMany(Products_model::class);
}
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function orders()
{
$this->hasManyThrough(Order::class, Products_model::class, 'buyer_id', 'seller_id', 'product_id');
}
public function buys() {
$this->hasMany(Order::class, 'buyer_id', 'id');
}
public function sells() {
$this->hasMany(Order::class, 'seller_id', 'id');
}
}
Products_model.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class products_model extends Model
{
protected $table='products';
protected $primaryKey='id';
protected $fillable=['seller_id','pro_name','pro_price','pro_info','image','stock','category_id'];
}
OrderProduct.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrderProduct extends Model
{
protected $table = 'order_product';
protected $fillable = ['order_id', 'product_id', 'quantity'];
public function products()
{
return $this->belongsTo('App\Products_model');
}
}
Order.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
//protected $table = 'orders';
protected $fillable = [
'buyer_id', 'seller_id','shipping_email', 'shipping_name', 'shipping_city', 'shipping_phone', 'billing_subtotal', 'billing_total',
];
public function user()
{
return $this->belongsTo('App\User');
}
public function products()
{
return $this->belongsToMany('App\Products_model')->withPivot('quantity');
}
public function orders()
{
return $this->hasMany('App\OrderProduct', 'order_id');
}
public function buyer()
{
return $this->belongsTo(User::class, 'id', 'buyer_id');
}
public function seller()
{
return $this->belongsTo(User::class, 'id', 'seller_id');
}
}
Вид продавца Функция заказа
public function viewOrders(User $user)
{
$products = Products_model::where('seller_id', '=', $user->id)->get();
// all sells
$sells = $user->sells;
// all buys
$buys = $user->buys;
}
//dd( $products);
return view('orders')->with(compact('orders'));
Продавец Панель приборов продавца
@foreach($sells as $sell)
<tr>
<td>{{$sell->orders}}</td>
<td>{{$sell->products}}</td>
@foreach($sell->orders as $order)
<td>{{$order->created_at}}</td>
<td>{{$order->shipping_name}}</td>
<td>{{$order->shipping_city}}</td>
<td>{{$order->shipping_phone}}</td>
<td>
<a href="">View Order Details</a>
</td>
</tr>