簡體   English   中英

Laravel 問題中的雄辯關系

[英]Eloquent relationships in laravel Problems

我在處理 Laravel 中的表關系時遇到了問題。 我有三個表 Orders 表、Order_product 表和 User 表。 用戶可以被描述為賣家或買家,具體取決於他們是否列出或購買了某些東西。 現在,當用戶提交訂單時,我收到一個錯誤

“一般錯誤:1364 字段‘seller_id’沒有默認值(SQL:插入到order_product ( order_id , product_id , quantity , `up ▶"

這是這些表在 phpmyAdmin 中的樣子

https://imgur.com/a/fvxo1YZ

下面是模型

用戶名

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

產品_型號

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

訂單產品.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!');
   }

錯誤非常具體:

一般錯誤:1364 字段 'seller_id' 沒有默認值(SQL:插入 order_product

查看您發布的代碼,假設它發生在這里:

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

當該字段沒有默認值或在 DB 中不可為空時,您不能在不為 Seller_id 提供值的情況下創建 OrderProduct。 所以,在創建記錄時給它一個值。 查看模型,我認為您可以執行以下操作:

$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,
]);

您需要發送賣家 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,
    ]);

或者您可以刪除 Order Table 中的 Seller_id,因為您可以從 Product Model 中獲取賣家信息

將模型發布到一對一的關系,將用戶模型發布到一對多的關系。

發布模型到一對一的關系,以及用戶模型到一對多的關系

public function categories(){
return $this->belongsToMany('App\Category')->withTimestamps();
}

public function posts(){
return $this->belongsToMany('App\Post')->withTimestamps();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM