【问题标题】:Eloquent relationships in laravel Problemslaravel 问题中的雄辩关系
【发布时间】:2019-10-29 13:26:14
【问题描述】:

我在如何处理 laravel 中的表关系时遇到问题。我有三个表 Orders 表、Order_product 表和 User 表。用户可以被描述为卖家或买家,具体取决于他们是否列出或购买了某物。现在,当用户提交订单时,我得到一个错误

“一般错误:1364 字段‘seller_id’没有默认值(SQL:插入order_product (order_id,product_id,quantity,`up ▶"

这是这些表在 phpmyAdmin 中的样子

https://imgur.com/a/fvxo1YZ

以下是模型

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

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

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

Products_model

<?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', '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');
}

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

订单.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)
        ]);
       }
    }

   //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!');
   }

【问题讨论】:

  • 我想在买家下单的时候得到一个卖家_id,这就是为什么它不为空,我不知道要添加什么,这样我才能得到卖家_id(我坚持那个)@ porloscerrosΨ
  • 但是,你如何获得seller_id?它在请求中吗?当该字段没有默认值或在 DB 中不可为空时,您无法在不向 Seller_id 提供值的情况下创建 OrderProduct
  • seller_id 与 User_id 相同,所以当用户注册并列出产品时,他的 Id 作为卖家进入产品表。

标签: php laravel laravel-5 eloquent


【解决方案1】:

您需要发送卖家 ID 的值。

  $order = Order::create([
        'buyer_id' => auth()->user() ? auth()->user()->id : null,
        'seller_id' => $request->seller_id,
        'shipping_email' => $request->email,
        'shipping_name' => $request->name,
        'shipping_city' => $request->city,
        'shipping_phone' => $request->phone,
       // 'error' => null,
    ]);

或者您可以删除订单表中的卖家 ID,因为您可以从产品模型中获取卖家信息

【讨论】:

  • 我希望每个卖家都能在他的仪表板中看到他的订单,这就是为什么我需要在订单上使用卖家 ID
  • 所以在添加'seller_id' =&gt; $request-&gt;seller_id, 之后我仍然收到错误'seller_id' doesn't have a default value
  • 你应该得到 OrderProduct 模型的数据
【解决方案2】:

错误非常具体:

一般错误:1364 字段“seller_id”没有默认值 (SQL: 插入 order_product

看看你发布的代码,假设它发生在这里:

OrderProduct::create([
    'order_id' => $order->id ?? null,
    'product_id' => $productId,
    'quantity' => $item['quantity'],
]);

如果 OrderProduct 字段没有默认值或在 DB 中不可为空,则无法创建 OrderProduct 而不给 Seller_id 赋值。因此,在创建记录时给它一个值。查看模型,我认为您可以执行以下操作:

$product = products_model::find($productId);
OrderProduct::create([
    'order_id' => $order->id ?? null,
    'product_id' => $productId,
    'quantity' => $item['quantity'],
    'seller_id' => $product->seller_id,
    'buyer_id' => $order->buyer_id,
]);

【讨论】:

  • 现在显示seller_id但不显示buyer_id(我想同时显示)@porlosc
  • 我想你可以做与 Order 相同的事情:'buyer_id' =&gt; auth()-&gt;user() ? auth()-&gt;user()-&gt;id : null。看看答案的更新
  • 很好,现在问题来了,当我希望每个卖家在他的仪表板中看到他自己的订单时(我为此付出了很多努力)让我用视图刀片和返回的函数更新问题查看@porlosc
  • 好吧,我认为这是题外话。您在尝试在数据库中保存记录时询问了特定错误。我不知道你必须在视图中显示这个问题是什么,但我认为你应该问另一个问题来指定问题。
【解决方案3】:

将模型发布到一对一的关系, 和用户模型建立一对多关系。

【讨论】:

    猜你喜欢
    • 2019-03-17
    • 2014-10-18
    • 1970-01-01
    • 2020-09-25
    • 2016-06-26
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多