如何从数据库Laravel的Products表中查询卖方ID [英] How to query Seller Id from Products table in database Laravel

查看:93
本文介绍了如何从数据库Laravel的Products表中查询卖方ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,用户可以在该项目中销售和购买产品,并且在我的数据库中,有两个表(orders和order products表),orders表中有Buyer_id和Seller_id.因此,如果用户购买产品,现在显示的是Buyer_id,则问题出在Seller_id上.它没有显示Seller_id.

I'm working on a project where users can sell and also buy products, and in my database there are two tables(orders and order products table)in orders tables there's a buyer_id and seller_id. So if a user buys product it shows buyer_id now the problem comes to seller_id. It doesn't show the seller_id.

这是我的代码.

User.php

 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()
 {
   return $this->hasManyThrough(Order::class, Products_model::class, 'buyer_id', 'seller_id', 'product_id');
  }

 public function orderFromBuyers()
 {
  $this->hasManyThrough(OrderProduct::class, Products_model::class, 'buyer_id', 'product_id');
 }

 public function orderFromSellers()
 {
    $this->hasManyThrough(OrderProduct::class, Products_model::class, 'seller_id', 'product_id');
 }
 }

Products_model.php

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

OrderProduct.php

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

class OrderProduct extends Model
{
protected $table = 'order_product';
protected $fillable = ['order_id', 'buyer_id', 'seller_id','product_id', 'quantity'];

public function products()
{
  return $this->belongsTo('App\Products_model');
}

public function buyer()
{
    return $this->belongsTo(User::class, 'id', 'buyer_id');
}

public function seller()
 {
    return $this->belongsTo(User::class, 'id', 'seller_id');
 }
  }

Order.php

Order.php

 <?php
 namespace App;
 use Illuminate\Database\Eloquent\Model;
 class Order extends Model
 {
//protected $table = 'orders';
protected $fillable =  [
    '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 store(Request $request)
   {
    //Insert into orders table
    $order = Order::create([
        'buyer_id' => auth()->user() ? auth()->user()->id : null,
        'shipping_email' => $request->email,
        'shipping_name' => $request->name,
        'shipping_city' => $request->city,
        'shipping_phone' => $request->phone,
       // 'error' => null,
    ]);

    //Insert into order product table
    if ($order) {
        foreach(session('cart')  as $productId =>$item) {
           if (empty($item)) {
               continue;
           }
           OrderProduct::create([
            'order_id' => $order->id ?? null,
            'product_id' => $productId,
           // $products=DB::table('products')->where('id',$id)->get();
            'quantity' => $item['quantity'],
            //dd($item)
        ]);
       }
    }

CheckoutController(函数)

CheckoutController(function)

    public function store(Request $request)
     {
    //Insert into orders table
    $order = Order::create([
        'buyer_id' => auth()->user() ? auth()->user()->id : null,
        'seller_id' => auth()->user() ? auth()->user()->id : null,            'shipping_email' => $request->email,
        'shipping_name' => $request->name,
        'shipping_city' => $request->city,
        'shipping_phone' => $request->phone,
       // 'error' => null,
    ]);

    //Insert into order product table
    if ($order) {
        foreach(session('cart')  as $productId =>$item) {
           if (empty($item)) {
               continue;
           }
           OrderProduct::create([
            'order_id' => $order->id ?? null,
            'product_id' => $productId,
           // $products=DB::table('products')->where('id',$id)->get();
            'quantity' => $item['quantity'],
            //dd($item)
        ]);
       }
    }

   //Empty Cart After  order created
    $cart = session()->remove('cart');
     return redirect()->route('confirmation.index')->with('success_message', 'Thank you! Your payment has been successfully accepted!');
     }

ProductController(函数)

ProductController(function)

    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>
@endforeach
@endforeach

推荐答案

将此添加到OrderProduct.php

public function order()
{
    return $this->belongsTo(Order::class);
}

更新产品控制器

 public function viewOrders(User $user)
  {

      // $products = Products_model::where('seller_id', '=', $user->id)->get();
      // all sells
      $sells = $user->orderFromSellers;
      return view('orders')->with(compact('sells'));

  }
  //dd( $products);

将您的视图更新为

 @foreach($sells as $sell) 
    <tr>
        <td>{{$sell->orders}}</td>
        <td>{{$sell->products}}</td>
        <td>{{$sell->created_at}}</td>
        <td>{{$sell->order->shipping_name}}</td>
        <td>{{$sell->order->shipping_city}}</td>
        <td>{{$sell->order->shipping_phone}}</td>
        <td>
            <a href="">View Order Details</a>
        </td>
    </tr>
@endforeach

在CheckoutController(存储)上更新为

On CheckoutController (store) update to,

public function store(Request $request)
     {
    //Insert into orders table
    $order = Order::create([          
        'shipping_email' => $request->email,
        'shipping_name' => $request->name,
        'shipping_city' => $request->city,
        'shipping_phone' => $request->phone,
       // 'error' => null,
    ]);

//Insert into order product table
if ($order) {
    foreach(session('cart')  as $productId =>$item) {
       if (empty($item)) {
           continue;
       }
       OrderProduct::create([
        'buyer_id' => auth()->user() ? auth()->user()->id : null,
        'seller_id' => $products=DB::table('products')->find('productId')? $products=DB::table('products')->find('productId')->seller_id : null,  
        'order_id' => $order->id ?? null,
        'product_id' => $productId,
       // $products=DB::table('products')->where('id',$id)->get();
        'quantity' => $item['quantity'],
        //dd($item)
    ]);
   }
}

   //Empty Cart After  order created
    $cart = session()->remove('cart');
     return redirect()->route('confirmation.index')->with('success_message', 'Thank you! Your payment has been successfully accepted!');
     }

这篇关于如何从数据库Laravel的Products表中查询卖方ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆