我希望卖家能够删除/编辑他列出的产品。但是,如果该产品已由客户订购,我希望订单保持不变而不改变。我该怎么做?
现在,当我更新/删除时,它会影响之前提交的订单。 这就是我现在删除产品的方式。
public function destroy($id)
{
$userId = Auth::user()->id;
$deleteData=product::where('seller_id', $userId)->findOrFail($id);
$deleteData->delete();
return redirect()->back();
}
Order.php
public function user()
{
return $this->belongsTo('App\User', 'seller_id');
}
public function products()
{
return $this->belongsToMany('App\Product')->withPivot('quantity','total','Subtotal');
}
public function orderItems()
{
return $this->hasMany('App\OrderProduct');
}
编辑:
Controller for seller orders
// Seller Orders
public function viewOrders(User $user)
{
//$seller = Auth::user();
$totals = OrderProduct::select("seller_id", DB::Raw("SUM(Subtotal) AS total"), 'order_id')
->where('seller_id', '=', \Auth::user()->id)
->groupBy('seller_id')
->groupBy('order_id')
->get();
$orders = Order::whereHas('orderItems.product', function ($query) {
$query->where('seller_id', '=', \Auth::user()->id);
})->orderBy('id', 'DESC')->withTrashed()->get();
return view('orders', ['orders'=> $orders, 'total'=> $totals] );
}
卖方刀片模板
@foreach ($order->orderItems as $item)
@if($item->product->user_id == Auth::user()->id)
<td>{{ $item->name }}</td>
<td>{{ $item->price }}</td>
@else
@endif
@endforeach
OrderProduct.php
class OrderProduct extends Model
{
use SoftDeletes;
protected $table = 'order_product';
protected $fillable = ['order_id', 'seller_id','product_id', 'quantity','Subtotal','total','name','price'];
public function product()
{
return $this->belongsTo('App\Product');
}
答案 0 :(得分:1)
将SoftDeletes
特征添加到您的产品模型中。
要检索包括已删除产品在内的产品,请使用以下方法:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrderProduct extends Model
{
protected $table = 'order_product';
protected $fillable = ['order_id', 'seller_id','product_id', 'quantity','Subtotal','total'];
public function product()
{
return $this->belongsTo('App\Product')->withTrashed();
}
}
如果您不想显示已删除的产品,则可以在检索产品的函数中添加->whereNull('p.deleted_at')
:
$products = DB::table('recommends AS r')
->leftJoin('products AS p', 'r.product_id', '=', 'p.id')
->join('products_photos AS pp', 'pp.product_id', '=', 'p.id')
->whereNull('p.deleted_at')
->select('p.id', 'p.pro_name', 'filename', 'p.pro_price', 'pro_info', DB::raw('COUNT(*) AS total'))
->groupBy('p.id', 'p.pro_name', 'filename', 'p.pro_price', 'pro_info')
->orderby('total', 'DESC')
->take(4);
它应该可以解决您的问题。
希望有帮助。