Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database I';我试图用一个表单在db中保存多条记录,但我有一个错误:数组到字符串的转换_Database_Laravel_Controller - Fatal编程技术网

Database I';我试图用一个表单在db中保存多条记录,但我有一个错误:数组到字符串的转换

Database I';我试图用一个表单在db中保存多条记录,但我有一个错误:数组到字符串的转换,database,laravel,controller,Database,Laravel,Controller,我试图用一个表单将多个产品的记录保存在一个表中,数据库中,但是,出现了这个错误,我不知道如何解决它 错误:数组到字符串的转换 在vendor\laravel\framework\src\light\Support\Str中 $result .= (array_shift($replace) ?? $search).$segment; “引用”和“数量”的数据类型为字符串 这是一种观点: <form action="{{route('carts.store')}}"

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

错误:数组到字符串的转换

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

   $result .= (array_shift($replace) ?? $search).$segment;
“引用”和“数量”的数据类型为字符串

这是一种观点:

<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>
当我点击“添加产品”按钮时,只有最后的记录保存在表中

类购物车扩展模型
{
受保护的$fillable=['id','reference','pdv_code','QUOTE'];
public$timestamps=false;
公共功能产品()
{
返回$this->belongtomany('App\Product');
}    
}
类产品扩展模型
{
受保护的$fillable=['ean','reference','product_price','pdv_code'];
public$timestamps=false;
公共功能详细信息()
{
返回$this->belongsTo('App\Products_detail');
}
公共功能车()
{
返回$this->belongstomy('App\Cart');
}
}
>大宗报价
迁移:

public function up()
{
架构::创建('cart_product',函数(Blueprint$表){
$table->unsignedbiginger('cart_id');
$table->foreign('cart\u id')
->引用('id')
->在(‘手推车’)上
->onDelete(“级联”);
$table->unsignedbiginger('product_id');
$table->foreign('product\u id')
->引用('id')
->关于(‘产品’)
->onDelete(“级联”);
$table->primary(['cart\u id','product\u id']);
});
}
公共职能部门
{
模式::dropIfExists('cart_product');
}
迁移购物车:

public function up()
{
Schema::create('carts',函数(Blueprint$表){
$table->id();
$table->char('reference');
$table->integer('数量');
$table->timestamps();
});
}
/**
*反向迁移。
*
*@返回无效
*/
公共职能部门
{
Schema::dropIfExists('carts');
}

您的表单应该通过两个输入字段的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>

@csrf
@foreach(会话('cart')作为$id=>$details)
@endforeach
添加产品
$request->input('cart\u items')
将给出添加到购物车的所有产品的数组

您需要另一个模型CartItem和表cart\u items来存储购物车的行项目,其中每行至少可以有一个产品id和数量

或者您可以在
购物车产品
透视表上添加
数量
价格

对于carts表:您可以有列:
total
tax
等产品参考和数量列不能在carts表上,因为它们没有任何用处-一个购物车可以添加3种产品,例如[Tshirt:1,裤子:2,连帽衫:1],现在哪个参考和产品的数量将存储在carts表中

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

Tshirt->id,数量,购物车id

裤子编号、数量、购物车编号


连帽衫->id,数量,购物车id

其中是$result.=(数组\u shift($replace)??$search)。$segment;电话是从哪里来的?购物车模型上引用列和数量列的数据类型是什么。其中是“添加产品”按钮的代码,我插入其中是错误,引用和数量的数据类型是字符串,我没有翻译按钮,表单的按钮类型是否为submit?提交表单时收到的数据具有引用和数量作为数组-并且您正试图使用字符串DataTypes分配到一列您实际上正在从表单发送多个购物车的数据,而您在控制器中只创建了一个新购物车-您希望做的是不清楚在购物车中,我有多个产品,我必须在数据库表中的相应行中记录一个产品,但记录在同一购物车中。
<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>