购物车更新laravel

时间:2016-12-26 14:22:41

标签: laravel laravel-5.1

有这样的表格。这是一个更新表单,我只需要在购物车中更新qty foreach产品。 但我试图爆炸每个结果,而不是工作...它返回object2array转换错误...它第一次我得到这个错误如何我可以在DB中保存?

class Mutable(object):  # Example class (unchangeable).
    def __init__(self):
        self.attr0 = 0
        self.attr1 = 1

    def __repr__(self):
        return str(self.__dict__)[1:-1]

class MonitoredMutable(Mutable):
    _get_callback = _set_callback = lambda *_: None  # no-op placeholders

    def __init__(self, get_callback, set_callback):
        # use superclass to avoid infinite recursion when setting attributes
        super_delegate = super(MonitoredMutable, self)
        super_delegate.__init__()
        super_delegate.__setattr__('_get_callback', get_callback)
        super_delegate.__setattr__('_set_callback', set_callback)

    def __setattr__(self, name, value):
        super(MonitoredMutable, self).__setattr__(name, value)
        self._set_callback(name, value)  # write notification

    def __getattr__(self, name):
        self._get_callback(name)  # read notification
        return super(MonitoredMutable, self).__getattr__(name, value)

    def __repr__(self):  # optional
        # override to only display the public attributes of the instance
        public_attrs = {k:v for k,v in self.__dict__.items()
                            if not k.startswith('_')}
        # assuming single inheritance (only one base class)
        base_classname = self.__class__.__bases__[0].__name__
        return base_classname + ': ' + (str(public_attrs)[1:-1] if public_attrs
                                        else 'No pub attributes')

class Foo(object):
    def __init__(self):
        self._mutable = MonitoredMutable(self._get_callback, self._set_callback)

    def _get_callback(self, name):
        print('mutable.' + name + ' was read')

    def _set_callback(self, name, value):
        print('mutable.' + name, 'was set to', value)

    @property
    def mutable(self):
        return self._mutable

    @mutable.setter
    def mutable(self, attr_value_pair):
        attribute, value = attr_value_pair
        setattr(self._mutable, attribute, value)

bar = Foo()
print(bar.mutable)           # -> Mutable: 'attr0': 0, 'attr1': 1
bar.mutable = ('attr0', 5)   # -> mutable.attr0 was set to 5
bar.mutable = ('attr1', 10)  # -> mutable.attr1 was set to 10
print(bar.mutable)           # -> Mutable: 'attr0': 5, 'attr1': 10

# These now work
bar.mutable.attr0 = 1        # -> mutable.attr0 was set to 1
bar.mutable.attr1 = 0        # -> mutable.attr1 was set to 0
print(bar.mutable)           # -> Mutable: 'attr0': 1, 'attr1': 0

这是我的控制者:

<form action="update">
  @foreach($products as $product)
<input type="text" name="products[]">
<input type="text" name="qty[]">
@endforeach
<input type="submit" calss="btn btn-primary">

提前致谢。

1 个答案:

答案 0 :(得分:-1)

您为数据库中存在的每个$carreli执行迭代,但是您永远不会保存该值。

Route::post('aggiorna', function() {
     $quantita = Input::all();
     $carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get();
     $quantita = explode(',', $quantita);
     $i = 0;
     foreach($carrelli as $carrello) {
        $carrello->quantita = $quantita[$i]; 
        $i = $i++;
        $carrelo->save();
     }
    return Redirect::to('carrello'); 

});

在更新值后添加 $carrelo->save(); ,以便将其保存在db上。

使用Input::all();时也要小心。这意味着您的数据阵列包含产品和数量。我建议使用以下代码:

Route::post('aggiorna', function() {
     $quantita = Input::get('qty');
     $products = Input::get('products');
     $carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get();
     $quantita = explode(',', $quantita);
     $products = explode(',', $products);
     $i = 0;
     foreach($carrelli as $carrello) {
        $carrello->quantita = $quantita[$i]; 
        $carrello->products = $products[$i]; 
        $i = $i++;
        $carrelo->save();
     }
    return Redirect::to('carrello'); 

});

然而,由于我不知道你想要达到的目的,我在这里发布了两个解决方案。

相关问题