产品信息未更新到数据库

时间:2014-11-24 12:09:43

标签: php laravel laravel-4

我正在开发购物车应用。 创建的产品存储在数据库中,这很好。 但是,产品不会更新到数据库。 我已经查看了这个并且无法找出原因,因为我在postEdit函数中将产品信息保存到db。

这是我的ProductsController.php

public function postEdit() {
    $product = Product::find(Input::get('id'));


if ($product) {
    $image = Input::file('image');
    $filename  = time() . '.' . $image->getClientOriginalExtension();
    $path = public_path('img/products/' . $filename);
    Image::make($image->getRealPath())->resize(468, 249)->save($path);
    $product->image = 'img/products/'.$filename;
    $product->save();
    $product->update(Input::except('image'));
    return Redirect::to('admin/products/index')
    ->with('message', 'Product Updated');
}

return Redirect::to('admin/products/index')
    ->with('message', 'Something went wrong, please try again');
}

这是我的Product.php模型

<?php

class Product extends Eloquent {

protected $fillable = array('category_id', 'title', 'description', 'price', 'availability', 'image');

public static $rules = array(
    'category_id'=>'required|integer',
    'title'=>'required|min:2',
    'description'=>'required|min:20',
    'price'=>'required|numeric',
    'availability'=>'integer',
    'image'=>'required|image|mimes:jpeg,jpg,bmp,png,gif'
);

public function category() {
    return $this->belongsTo('Category');
}
}

这是我的index.php for products view

@extends('layouts.main')

@section('content')

<div id="admin">

    <h1>Products Admin Panel</h1><hr>

    <p>Here you can view, edit, delete, and create new products.</p>

    <h2>Products</h2><hr>

    <ul>
        @foreach($products as $product)
            <li>
                {{ HTML::image($product->image, $product->title, array('width'=>'50')) }} 
                {{ $product->title }} - 
                {{ Form::open(array('url'=>'admin/products/destroy', 'class'=>'form-inline', 'files'=>true)) }}
                {{ Form::hidden('id', $product->id) }}
                {{ Form::submit('delete', array('onclick'=>'return checkDelete()')) }}
                {{ Form::close() }} - 

                {{ Form::open(array('url'=>'admin/products/toggle-availability', 'class'=>'form-inline', 'files'=>true))}}
                {{ Form::hidden('id', $product->id) }}
                {{ Form::select('availability', array('1'=>'In Stock', '0'=>'Out of Stock'), $product->availability) }}
                {{ Form::submit('Update') }}
                {{ Form::close() }}

                {{ Form::open(array('url'=>'admin/products/edit', 'files'=>true, 'class'=>'form-inline', 'files'=>true))}}
                {{ Form::hidden('id', $product->id) }}
                {{ Form::label('title') }}
                {{ Form::text('title', $product->title) }}

                {{ Form::label('description') }}
                {{ Form::textarea('description', $product->description) }}

                {{ Form::label('price') }}
                {{ Form::text('price', $product->price, null, array('class'=>'form-price')) }}

                {{ Form::label('image', 'Choose an image') }}
                {{ Form::file('image') }}
                {{ Form::submit('edit') }}
                {{ Form::close() }}
            </li>
        @endforeach
    </ul>

    <h2>Create New Product</h2><hr>

    @if($errors->has())
    <div id="form-errors">
        <p>The following errors have occurred:</p>

        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div><!-- end form-errors -->
    @endif

    {{ Form::open(array('url'=>'admin/products/create', 'files'=>true)) }}
    <p>
        {{ Form::label('category_id', 'Category') }}
        {{ Form::select('category_id', $categories) }}
    </p>
    <p>
        {{ Form::label('title') }}
        {{ Form::text('title') }}
    </p>
    <p>
        {{ Form::label('description') }}
        {{ Form::textarea('description') }}
    </p>
    <p>
        {{ Form::label('price') }}
        {{ Form::text('price', null, array('class'=>'form-price')) }}
    </p>
    <p>
        {{ Form::label('image', 'Choose an image') }}
        {{ Form::file('image') }}
    </p>
    {{ Form::submit('Create Product', array('class'=>'secondary-cart-btn')) }}
    {{ Form::close() }}
</div><!-- end admin -->

@stop

这是我的数据库表信息:

public function up()
{
    Schema::create('products', function($table){
        $table->increments('id');
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories');
        $table->string('title');
        $table->text('description');
        $table->decimal('price', 6, 2);
        $table->boolean('availability')->default(1);
        $table->string('image');
        $table->timestamps();
    });
}

1 个答案:

答案 0 :(得分:0)

我认为您需要交换更新并保存函数:

$product->update(Input::except('image'));
$product->save();