Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.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
Php laravel pivot表唯一_Php_Sql_Laravel - Fatal编程技术网

Php laravel pivot表唯一

Php laravel pivot表唯一,php,sql,laravel,Php,Sql,Laravel,订单id…产品id…产品数量…创建时间…更新时间 这是我的数据透视表…在我的OrderRequest表中,我想将product_id指定为唯一。但是当我这样写的时候 public function rules() { return [ 'product_id' => 'unique:order_product,product_id' ]; } 我遇到了一个问题。产品id变

订单id…产品id…产品数量…创建时间…更新时间

这是我的数据透视表…在我的OrderRequest表中,我想将product_id指定为唯一。但是当我这样写的时候

public function rules()
    {            
        return [
                'product_id' => 'unique:order_product,product_id'
                ];
   }

我遇到了一个问题。产品id变得唯一。但不仅仅是在一个秩序中,它变得完全独一无二。我想在其他订单中使用此产品,但我不能。我能做什么?如何为每个订单id值分配唯一的产品id?

在控制器中,假设您调用
saveProducts
方法,则:

function saveProducts($request){
    validateProductList($request->input('product_list'))
    $order->product()
          ->updateExistingPivot($product_id[$i], 
                array( 
                    'product_quantity' => $product_quantity[$i], 
                    'product_status' => $product_status[$i], 
                    'updated_at' => Carbon::now() ));
    // ....
}

function validateProductList($productIds){
    if(isProductIdDuplicated($productIds)){
        $error = 'You have a duplicated product in your Order, please edit it'
        return redirectBack()-withError();
    }
}

function isProductIdDuplicated($productIds){
    $occureces_array = productIds();
    foreach($productIds as $id){
        if(++$occureces_array[$id] > 1){
            return true;
        }
    }
    return false;
}   

在您看来,您可以访问控制器中的
$error
变量。

假设您调用
saveProducts
方法,那么:

function saveProducts($request){
    validateProductList($request->input('product_list'))
    $order->product()
          ->updateExistingPivot($product_id[$i], 
                array( 
                    'product_quantity' => $product_quantity[$i], 
                    'product_status' => $product_status[$i], 
                    'updated_at' => Carbon::now() ));
    // ....
}

function validateProductList($productIds){
    if(isProductIdDuplicated($productIds)){
        $error = 'You have a duplicated product in your Order, please edit it'
        return redirectBack()-withError();
    }
}

function isProductIdDuplicated($productIds){
    $occureces_array = productIds();
    foreach($productIds as $id){
        if(++$occureces_array[$id] > 1){
            return true;
        }
    }
    return false;
}   

在您看来,您可以访问此
$error
变量。

感谢您的帮助。我已经结束了这个问题,它现在起作用了

这是我观点的一部分

<div class="grid1">
   {!! Form::label('product_list'.$product_row, Lang::get('home.Select Product')) !!}         
</div>
<div class="grid2 searchDrop"> 
   {!! Form::select('product_list[]', $product_list, null, ['style'=>'width:150px;'])!!}
这是我的订单要求

public function rules()
    {
        return [
            'customer_id' => 'required',
            'product_quantity.*' =>'not_in:0',
            'product_list' => 'product_unique',  //custom validation in AppServiceProvider
       ];
    }
这是我的AppServiceProvider

public function boot()
    {
       Validator::extend('product_unique', function($attribute, $value, $parameters, $validator) {
                if ( count(array_unique($value))!=count($value) ) {
                    return false;
                }
            return true;
        });
    }
最后,我在validation.php中添加了

"product_unique" => "Please select a product only once!",

谢谢你的帮助。我已经结束了这个问题,它现在起作用了

这是我观点的一部分

<div class="grid1">
   {!! Form::label('product_list'.$product_row, Lang::get('home.Select Product')) !!}         
</div>
<div class="grid2 searchDrop"> 
   {!! Form::select('product_list[]', $product_list, null, ['style'=>'width:150px;'])!!}
这是我的订单要求

public function rules()
    {
        return [
            'customer_id' => 'required',
            'product_quantity.*' =>'not_in:0',
            'product_list' => 'product_unique',  //custom validation in AppServiceProvider
       ];
    }
这是我的AppServiceProvider

public function boot()
    {
       Validator::extend('product_unique', function($attribute, $value, $parameters, $validator) {
                if ( count(array_unique($value))!=count($value) ) {
                    return false;
                }
            return true;
        });
    }
最后,我在validation.php中添加了

"product_unique" => "Please select a product only once!",

您可以通过编辑pivot迁移来实现这一点,并添加此行
$table->primary(['order\u id','product\u id')
如果需要,可以从透视表中删除
id
列。别忘了做
artisan迁移:刷新
!!没关系,但我还需要别的东西。在请求表单中,当我两次选择一个产品时,应该会收到一条错误消息。当我像你说的那样尝试时,它不会给我一条错误消息,只是将同一产品的最后一个选项保存到数据库。看看这个它可以帮助你,如果你需要一些解释,请告诉我:)我知道那篇文章。但情况不同。在那个例子中,用户是静态的,但在我的例子中,订单是变化的。订单id在控制器中创建。因此,如果你能详细阐述更多细节来充分了解你的需求,你会如何选择产品?在您的控制器中,您可以得到所选产品的列表,或者确切地说是什么。。。您可以通过编辑pivot迁移来实现这一点,并添加此行
$table->primary(['order\u id','product\u id')
如果需要,可以从透视表中删除
id
列。别忘了做
artisan迁移:刷新
!!没关系,但我还需要别的东西。在请求表单中,当我两次选择一个产品时,应该会收到一条错误消息。当我像你说的那样尝试时,它不会给我一条错误消息,只是将同一产品的最后一个选项保存到数据库。看看这个它可以帮助你,如果你需要一些解释,请告诉我:)我知道那篇文章。但情况不同。在那个例子中,用户是静态的,但在我的例子中,订单是变化的。订单id在控制器中创建。因此,如果你能详细阐述更多细节来充分了解你的需求,你会如何选择产品?在您的控制器中,您可以得到所选产品的列表,或者确切地说是什么。。。这符合你的需要吗?这符合你的需要吗?