我正在尝试使用表单在数据库中保存多条记录,但是出现此错误: 数组到字符串的转换



我试图用一个表单将多个产品的记录保存在一个表、数据库中,但出现了这个错误,我不知道如何解决它。

错误:数组到字符串转换

在vendor\laravel\framework\src\Illuminate\Support\Str 中

$result .= (array_shift($replace) ?? $search).$segment;

"reference"one_answers"quantity"的数据类型为字符串。

这是视图:

<form action="{{route('carts.store')}}" method="post">
@csrf
@foreach(session('cart') as $id => $details)
<div class="form-group">
<input type=hidden class="form-control" name="reference[]" id="referenceNumber" value="{{ $details['reference'] }}">
</div>
<div class="form-group">
<input type=hidden class="form-control quantity" name="quantity[]" value="{{$details['quantity']}}" id="productPrice">
</div> 

@endforeach
<button type="submit" class="btn btn-primary">Add products</button>
</form>

这是控制器:

public function store(Request $request)
{   
$cart = new Cart;
$data = [
'reference' => $request->reference,
'quantity' => $request->quantity
];
$cart->fill($data);
$cart->save();
return view('riepilogo');
}

当我点击按钮"时;添加产品";只有最后的记录保存在表中

class Cart extends Model
{
protected $fillable = ['id', 'reference', 'pdv_code', 'quantity'];
public $timestamps = false;
public function product()
{
return $this->belongsToMany('AppProduct');
}    
}
class Product extends Model
{
protected $fillable = ['ean', 'reference', 'product_price', 'pdv_code'];
public $timestamps = false;
public function detail() 
{
return $this->belongsTo('AppProducts_detail');
}
public function cart()
{
return $this->belongsToMany('AppCart');
}
}

> Blockquote

迁移:

public function up()
{
Schema::create('cart_product', function (Blueprint $table) {
$table->unsignedBigInteger('cart_id');
$table->foreign('cart_id')
->references('id')
->on('carts')
->onDelete('cascade');
$table->unsignedBigInteger('product_id');
$table->foreign('product_id')
->references('id')
->on('products')
->onDelete('cascade');
$table->primary(['cart_id', 'product_id']);
});
}
public function down()
{
Schema::dropIfExists('cart_product');
}

迁移车:

public function up()
{
Schema::create('carts', function (Blueprint $table) {
$table->id();
$table->char('reference');
$table->integer('quantity');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('carts');
}
public function addToCart(Request $request, $id, $reference)
{
$product = Product::find($id)->where('reference', $reference)->first();
if(!$product) {
abort(404);
}

$cart = session()->get('cart');
// if cart is empty then this the first product
if(!$cart) {
$cart = [
$id => [
"id" => $product->id,
"quantity" => 1,
"price" => $product->product_price,
"ean" => $product->ean,
"reference" => $product->reference
]
];
//dd($cart);
session()->put('cart', $cart);
return back()->with('success', 'Product added to cart successfully!');
}
// if cart not empty then check if this product exist then increment quantity
if(isset($cart[$id])) {
$cart[$id]['quantity']++;
session()->put('cart', $cart);
return back()->with('success', 'Product added to cart successfully!');
}
// if item not exist in cart then add to cart with quantity = 1
$cart[$id] = [
"id" => $product->id,
"quantity" => 1,
"price" => $product->product_price,
"ean" => $product->ean,
'reference' => $product->reference
];
session()->put('cart', $cart);
return back()->with('success', 'Product added to cart successfully!');
}

您的表单应该通过两个输入字段的id引用购物车

<form action="{{route('carts.store')}}" method="post">
@csrf
@foreach(session('cart') as $id => $details)
<div class="form-group">
<input type=hidden class="form-control" name="{{ 'cart_items[' .$id . '][reference]' }}" id="referenceNumber" value="{{ $details['reference'] }}">
</div>
<div class="form-group">
<input type=hidden class="form-control quantity" name="{{ 'cart_items[' . $id . '][quantity]' }}" value="{{$details['quantity']}}" id="productPrice">
</div> 

@endforeach
<button type="submit" class="btn btn-primary">Add products</button>
</form>

$request->input('cart_items')将给出添加到购物车的所有产品的阵列

您需要另一个模型CartItem和表cart_items来存储购物车的行项目,其中每行至少可以有一个product_id和数量。

也可以在cart_product数据透视表上添加quantityprice列。

对于购物车表:您可以有列:totaltax等。产品参考和数量列不能在购物车表上,这是没有用的——例如,一辆购物车可以添加3种产品[Tshirt:1,Trouser:2,Hoodie:1]现在,哪个参考和哪个产品的数量将存储在购物车中?

相反,cart_items表可以有三条记录

T恤->id,数量,cart_id

裤子id,数量,cart_id

连帽衫->id,数量,cart_id

最新更新