【问题标题】:Querying relationships in Laravel在 Laravel 中查询关系
【发布时间】:2019-06-14 17:09:09
【问题描述】:

我有一个 Order 表,其中包含 Buyer_id 和 Seller_id 作为列。买家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>

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    您的 products 表应该与产品的卖家有关系,并且在 order 表中您应该有 product_id 而不是 seller_id。那么当你创建一个订单的时候,你应该把买家正在购买的产品和卖家通过产品关系在那里。我希望我很清楚,这就是你要找的。​​p>

    甚至更多,而我正在考虑它。我不知道您对订单/产品的想法,但我想产品的订单应该像大多数商店一样是多对多的关系。意味着一个订单包含许多产品。

    卖家通过 products 模型有很多订单。所以如果一个产品属于卖家,那么你可以在你的卖家模型中做这个:

    public function orders()
    {
        return $this->hasManyThrough(Order::class, Product::class);
    }
    

    您可以在此here 上阅读更多信息。

    【讨论】:

    • 让我添加模型,以便您了解我是如何定义关系的。
    • 我不太了解你这里的业务逻辑。但是如果用户下单的产品属于一个卖家,那么你可以在刀片表单中设置一个隐藏字段,并通过那里传递卖家 ID。对我来说,如果产品来自不同的卖家,那么在订单表中包含 Seller_id 是没有意义的。
    • 逻辑是,我有可以销售(列出产品)和买家(可以从卖家那里购买的用户)的用户。我想要实现的是,当买家提交订单时,卖家应该能够看到。这就是我卡住的地方。
    • 但是所有的产品都是来自同一个卖家吗?
    • 不,不同的卖家。
    猜你喜欢
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 2021-08-30
    • 2013-10-03
    • 2017-07-19
    相关资源
    最近更新 更多