我有一个Order表,其中有Buyer_id和Seller_id作为列。 Buyer_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>
答案 0 :(得分:0)
您的产品表应与产品卖方具有关系,在订单表中应具有product_id
而不是seller_id
。然后,当您创建订单时,您应该放置买方购买的产品,而卖方将通过产品关系到达那里。我希望我很清楚,这就是您想要的。
还有更多,而我正在考虑。我不知道您对订单/产品的想法,但我想对产品的订单应该像大多数商店一样是多对多的关系。意味着订单包含许多产品。
卖方通过产品模型获得许多订单。因此,如果产品属于卖方,则可以在卖方模型中做到这一点:
public function orders()
{
return $this->hasManyThrough(Order::class, Product::class);
}
您可以阅读有关此here的更多信息。