2014-11-24 7 views
0

Я работаю над приложением для покупок. Созданные продукты хранятся в базе данных, и это отлично работает. Однако продукты не обновляются в базе данных. Я просмотрел это и не могу понять, почему, потому что я сохраняю информацию о продукте в db в своей функции postEdit.Информация о товаре не обновляется в базе данных

Вот мой 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 для продуктов просмотра

@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(); 
    }); 
} 

ответ

0

Я думаю, вам нужно поменять обновления и сохранения функции вокруг:

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

Хмм, я попробовал, что и никаких изменений. http://laravel.io/bin/OeOn2 – Bobby

Смежные вопросы