php - laravel 问题中的雄辩关系
问题描述
我在如何处理 laravel 中的表关系时遇到问题。我有三个表 Orders 表、Order_product 表和 User 表。用户可以被描述为卖家或买家,具体取决于他们是否列出或购买了某物。现在,当用户提交订单时,我得到一个错误
“一般错误:1364 字段 'seller_id' 没有默认值(SQL:插入order_product
( order_id
, product_id
, quantity
, `up ▶"
这是这些表在 phpmyAdmin 中的样子
以下是模型
用户.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'];
}
订单产品.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,
]);
推荐阅读
- c++ - 在数组c ++上制作6位引脚
- imagemagick-convert - 试图转换/展平/合并来自不同文件夹的所有图像。图像魔术
- coldfusion - CreateObject() 无法解析 CF2018 中的 CFC 路径
- javascript - Webpack、导出和 ES 模块:ReferenceError:模块未在 ES 模块范围内定义
- arrays - 如何在Mongodb中获取过滤后数组的大小
- javascript - 调用 applyActionCode setAccountInfo 时的 Firebase 身份验证
- azure - 如何在 azure devops 中创建自定义图表
- javascript - 选择标签并排选项标签
- flutter - 如何绘制一个 3 个四分之一圆用作颤动中的边框?
- html - EJS IF 语句内的 HTML 标记