Laravel多个属性创建

时间:2016-05-02 14:16:46

标签: php jquery laravel

我有我的laravel电子商店,我有类似产品属性的功能,我需要创建多个属性,但我有如下错误:

" ErrorException(E_WARNING) 救命 preg_replace():参数不匹配,pattern是一个字符串,而replacement是一个数组"

在这里,我将分享我的整个MVC结构,任何人都可以找到解决方案来解决该错误,提前谢谢,

我的控制器:

public function store()
{
    $attribute_values = Input::all();
    $attribute_price_values = Input::get('price');
    $image = Input::file('image');
    $attribute_id = Input::get('attribute_id');
    if($attribute_id <= 0) {
        return Redirect::back();
    }
    $attributes_data = Attribute::where('id', '=', $attribute_id)->get(array('price_mode'))->toArray();
    $attr_price = $attributes_data[0]; 
    foreach($attribute_values as $k => $v) {
        $insert_data = new AttributeValues;
        $insert_data->attribute_id = $attribute_id;
        $data_validation['attribute_id'] = $insert_data->attribute_id;
        $insert_data->value = $v;
        $data_validation['value'] = $insert_data->value;
        $validator = Validator::make($data_validation, AttributeValues::$rules);
        if ($validator->fails()) {
            $messages = $validator->messages();
            return Redirect::back()->withErrors($validator)->withInput();
        } else{
            //check the price flag

            //upload image in  directory
            // $attribute_value_image = $image[$k];
            if(isset($attribute_value_image) && $attribute_value_image != '') {
                $filename = time(). '_' .$attribute_value_image->getClientOriginalName();
                if (!file_exists(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'attribute_values' . DIRECTORY_SEPARATOR . 'original' . DIRECTORY_SEPARATOR .
                 $insert_data->attribute_id)) {
                    mkdir(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'attribute_values' . DIRECTORY_SEPARATOR . 'original' . DIRECTORY_SEPARATOR .
                        $insert_data->attribute_id, 0777, true);
                }
                if (!file_exists(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'attribute_values' . DIRECTORY_SEPARATOR . 'small' . DIRECTORY_SEPARATOR .
                     $insert_data->attribute_id)) {
                    mkdir(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'attribute_values' . DIRECTORY_SEPARATOR . 'small' . DIRECTORY_SEPARATOR .
                     $insert_data->attribute_id, 0777, true);
                }
                $original_image = Image::make($attribute_value_image->getRealPath())->save(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'attribute_values' . DIRECTORY_SEPARATOR . 'original' . DIRECTORY_SEPARATOR . $insert_data->attribute_id. DIRECTORY_SEPARATOR . $filename);

                $resized_small_image = Image::make($attribute_value_image->getRealPath())->resize(120, 44)->save(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'attribute_values' . DIRECTORY_SEPARATOR . 'small' . DIRECTORY_SEPARATOR . $insert_data->attribute_id . DIRECTORY_SEPARATOR . $filename);

                $fileFrom = public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'attribute_values' . DIRECTORY_SEPARATOR . 'original' . DIRECTORY_SEPARATOR .
                $insert_data->attribute_id . DIRECTORY_SEPARATOR . $filename;

                $insert_data->image = $filename;
            }

            if($attr_price['price_mode'] == 'Common') {
                $insert_data->price = $attribute_price_values[$k];
            }
            $insert_data->save();
        }
    }

    if(Input::get('stay') == 1){
        return Redirect::back();
    } else {
        return Redirect::route('backend.attribute_values.index');
    }
}

我的模特:

<?php

class AttributeValues extends \Eloquent {

protected $table = 'attribute_values';

protected $fillable = ['attribute_id','value', 'image', 'price','color'];
public static $rules = array(
    'attribute_id' => 'required',
    'value' => 'required',

);
public static $pricerules = array(
    'value' => 'required',
    'price' => 'required|regex:/^\d*(\.\d{2})?$/'
);
public static $edit_rules = array(
    'value' => 'required'
);

  public function attribute(){
    return $this->belongsTo('Attribute');
  }
}

我的观点:

@extends('layouts.backend')
@section('content')
<div class="row">
  <div class="col-xs-12">
    <div class="box">
        <div class="box-header">
            <i class="fa fa-cogs"></i>
            <h3 class="box-title">Add Attribute Values</h3>
            <div class="box-tools pull-right" data-toggle="tooltip" title="" data-original-title="Go Back">
                <li class="pull-right"><a href="    {{URL::route('backend.attribute_values.index')}}" class="text-muted"><i class="fa fa-chevron-left"></i> Go Back</a></li>
            </div>
        </div>
        {{ Form::model($attribute_values, ['route' => ['backend.attribute_values.create'],  'method' => 'post', 'enctype' => 'multipart/form-data']) }}

        <div class="box-body">
            @if ($errors->all() != null)
            <ul class="error">
                @foreach($errors->all() as $err)
                <li>{{$err}}</li>
                @endforeach
            </ul>
            @endif
            <?php
            $attributes[0] = '';
            foreach($attribute_names as $attribute_name) {
                $attributes[$attribute_name->id] =  $attribute_name->group_name.' > '.$attribute_name->name;
                $attribute_js_names[$attribute_name->id] = $attribute_name->price_mode;
                $attribute_js_price_val[$attribute_name->id] = $attribute_name->price_value;
            }
            ?>
            <div class="form-group">
                <table class="table table-bordered table-hover" id="tab_logic">
                    <tr id='addr0'>
                        <td>{{ Form::label('attribute_id', 'Attrbute Name*',array('data-toggle'=>"tooltip", 'data-original-title'=>"Attribute Type is required")) }}</td>
                        <td colspan="3">{{ Form::select('attribute_id', $attributes, null, array('class'=> 'ShowHidePrice')) }} {{ Form::hidden('sel_attribute_name', 'false', array('id'=>'sel_attribute_name')) }} {{ Form::hidden('sel_attribute_pval', 'Fixed', array('id'=>'sel_attribute_pval')) }}</td>

                    </tr>
                        <tr id='addr3'>
                            <td>
                                {{ Form::label('color[]', 'Attribute Value 1',array('data-toggle'=>"tooltip", 'data-original-title'=>"Value for selected attribute")) }}
                            </td>
                            <td>
                                {{ Form::label('color', 'Color:',array('data-toggle'=>"tooltip", 'data-original-title'=>"Color is required")) }}
                                {{ Form::input('color','color[]',null, array('class' => 'form-control-color','placeholder' => 'Enter Color','id' => 'exampleInputTitle1')) }}
                            </td>                               
                            <td class="attribute_price">
                                <span class="valpricesym"></span> {{ Form::text('price[]',null, array('class' => 'form-control form-control-extend','placeholder' => 'Enter Price for Attribute Value')) }} <span class="valpriceper"></span>
                            </td>
                        </tr>

                        <tr id='addr1'>
                            <td>
                                {{ Form::label('value', 'Attribute Value 1',array('data-toggle'=>"tooltip", 'data-original-title'=>"Value for selected attribute")) }}
                            </td>
                            <td>
                                {{ Form::text('value[]',null, array('class' => 'form-control','placeholder' => 'Enter Attribute Value')) }}
                            </td>
                            <td>
                                {{ Form::file('image[]') }}
                            </td>
                            <td class="attribute_price">
                                <span class="valpricesym"></span> {{ Form::text('price[]',null, array('class' => 'form-control form-control-extend','placeholder' => 'Enter Price for Attribute Value')) }} <span class="valpriceper"></span>
                            </td>
                        </tr>
                        <tr id='addr2'></tr>

                </table>
            </div>
            <div class="form-group">
                    <a id="add_row" class="btn btn-default pull-left">Add Row</a>
                    <a id='delete_row' class="pull-right btn btn-default">Delete Row</a>

            </div>
            <div class="form-group">&nbsp;</div>
            <div class="box-footer">
                {{ Form::button('Submit',array('class'=>'btn btn-primary','value' => '0', 'name' => 'stay', 'type' => 'submit')) }}
                {{ Form::button('Save & Stay',array('class'=>'btn btn-primary','value' => '1', 'name' => 'stay', 'type' => 'submit')) }}
            </div>
        </div>
    </div>
  </div>
</div>
{{ Form::close() }}
@stop
@section('javascript')

$(document).ready(function(){
    var i=2;
    var jsrr = new Array();
    var jspv = new Array();
    <?php 

        foreach($attribute_js_names as $k => $v) {
            echo 'jsrr['.$k.']="'.$v.'";';
            echo 'jspv['.$k.']="'.$attribute_js_price_val[$k].'";';
            //echo '\n';
        }
    ?>
    //alert(jsrr.length)
    $("#add_row").click(function(){
        var price_fld_vis = $('#sel_attribute_name').val();
        if(price_fld_vis == 'true') {
            dis_play = 'block';
        } else {
            dis_play = 'none';
        }
        var colors = $('#attribute_id').val();

        if(colors == 2){
            new_row = '<td><label data-original-title="Value for selected attribute" data-toggle="tooltip" for="value">Attribute Value '+(i)+'</label></td><td><input name="color[]" type="color" class="form-control-color"></span></td><td class="attribute_price" style="display:'+dis_play+'"><span class="valpricesym"></span>';
        }else{

        new_row = '<td><label data-original-title="Value for selected attribute" data-toggle="tooltip" for="value">Attribute Value '+(i)+'</label></td><td><input type="text" name="value[]" placeholder="Enter Attribute Value" class="form-control"></td><td><input name="image[]" type="file"></td><td class="attribute_price" style="display:'+dis_play+'"><span class="valpricesym"></span> <input type="text" name="price[]" placeholder="Enter Price for Attribute Value" class="form-control form-control-extend"> <span class="valpriceper"></span></td>';

        }

        $('#addr'+i).html(new_row);
        i++; 
        $('#tab_logic').append('<tr id="addr'+i+'"></tr>');
        updateCurrency();
    });
    $("#delete_row").click(function(){
        if(i>2){
            $("#addr"+(i-1)).html('');
            i--;
        }
    });
    $( ".ShowHidePrice" ).bind( "change", function() {

        //alert('calling...');
        var curval = $(this).val();
        if(jsrr[curval] == 'Common') {
            $('.attribute_price').show();
            attr_flag = 'true';
        } else {
            $('.attribute_price').hide();
            attr_flag = 'false';
        }
        attr_pval = jspv[curval]
        $('#sel_attribute_name').val(attr_flag);
        $('#sel_attribute_pval').val(attr_pval);
        updateCurrency();
    });
    function updateCurrency()
    {
        var pval_fld = $('#sel_attribute_pval').val();

        if(pval_fld == 'Fixed') {
            curr_sym = '£'
            val_sym = ''
        } else {
            curr_sym = ''
            val_sym = '%'
        }
        $('.valpriceper').html(val_sym);
        $('.valpricesym').html(curr_sym)
    }
});

        $("#addr3").hide();
        $("#addr1").show();
        $('#attribute_id').change(function(){               
            if($(this).val() == 2){
                $("#addr3").show();
                $("#addr1").hide();
            }else{
                $("#addr3").hide();
                $("#addr1").show();
            }


        });

  @stop

这里我使用的是attribute_id = 2,颜色属性是使用一些Jquery来隐藏和显示属性id将是2的颜色属性,所以总是显示错误像 &#34; ErrorException(E_WARNING) 救命 preg_replace():参数不匹配,pattern是一个字符串,而replacement是一个数组&#34;

1 个答案:

答案 0 :(得分:0)

请检查此链接,了解正确的解决方法:https://laravel.com/docs/5.1/eloquent 它将与多对多的关系一起工作。

相关问题