2017-07-11 98 views
0

我有一個產品表和product_galleries表,它們通過連接在一起有很多關係,當我嘗試添加一個新的產品來自後端, PS: 該錯誤只出現在我的生活網站上,在我的本地環境中後端工作正常,我可以添加儘可能多的產品,我想要的。Laravel 5.4 SQLSTATE [23000]:完整性約束違規:1062重複條目'0'現場網站

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' (SQL: insert into `products` (`product_name`, `product_description`, `product_preview`, `category_id`, `color_id`, `size_id`, `material_id`, `fantasia_id`, `slug`, `updated_at`, `created_at`) values (Test 2, , 1499770479-Petronius0039.jpg, 1, , , , , test-2, 2017-07-11 10:54:39, 2017-07-11 10:54:39)) 

產品表

<?php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateProductsTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('products', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('product_name'); 
      $table->string('product_preview')->nullable(); 
      $table->text('product_description')->nullable(); 
      $table->integer('category_id')->nullable()->unsigned(); 
      $table->integer('color_id')->nullable()->unsigned(); 
      $table->integer('material_id')->nullable()->unsigned(); 
      $table->integer('size_id')->nullable()->unsigned(); 
      $table->integer('model_id')->nullable()->unsigned(); 
      $table->integer('fantasia_id')->nullable()->unsigned(); 
      $table->string('slug'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::dropIfExists('products'); 
    } 
} 

product_galleries表

<?php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateProductGalleriesTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('product_galleries', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('product_id')->unsigned(); 
      $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); 
      $table->string('product_images')->nullable(); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::dropIfExists('product_galleries'); 
    } 
} 

產品型號

<?php 

namespace App; 

use Cviebrock\EloquentSluggable\Sluggable; 
use Illuminate\Database\Eloquent\Model; 
use App\Category; 
use App\Color; 
use App\Size; 
use App\Material; 
use App\Fantasia; 
use App\ProductGallery; 

class Product extends Model 
{ 
    use Sluggable; 
    protected $guarded = ['id']; 
    protected $table = 'products'; 
    public function sluggable() 
    { 
     return [ 
      'slug' => [ 
       'source' => 'product_name' 
      ] 
     ]; 
    } 

    public function categories(){ 
     return $this->belongsTo(Category::class, 'category_id'); 
    } 
    /*Connect the product table with the image table as one product can have many images*/ 
    public function images(){ 
     return $this->belongsToMany(Image::class , 'image_product'); 
    } 

    public function productgalleries(){ 
     return $this->hasMany(ProductGallery::class); 
    } 

    public function colors(){ 
     return $this->belongsTo(Color::class, 'color_id'); 
    } 

    public function fantasias(){ 
     return $this->belongsTo(Fantasia::class, 'fantasia_id'); 
    } 

    public function materials(){ 
     return $this->belongsTo(Material::class , 'material_id'); 
    } 

    public function sizes(){ 
     return $this->belongsTo(Size::class, 'size_id'); 
    } 

    public function newProducts(){ 
     return $this->hasOne(Size::class, 'size_id'); 
    } 
} 

產品的畫廊模式

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use App\Product; 

class ProductGallery extends Model 
{ 
    protected $table = 'product_galleries'; 
    protected $fillable = [ 
    'product_id', 
    'product_images' 
    ]; 
    public function products(){ 
     return $this->belongsTo(Product::class, 'product_id'); 
    } 
} 

產品控制器

<?php 

namespace App\Http\Controllers; 

use App\Product; 
use App\ProductGallery; 
use App\Category; 
use App\Color; 
use App\Image; 
use App\Size; 
use App\Material; 
use App\Fantasia; 
use App\Productgalleries; 
use DB; 
use File; 
use Illuminate\Support\Facades\Input; 
use Illuminate\Http\Request; 
use App\Http\Requests\UploadRequest; 

class ProductsController extends Controller 
{ 
    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     $products = Product::with('categories', 'colors' , 'sizes', 'materials' , 'fantasias')->get(); 
     return view('backend.product.product-library', compact('products')); 
    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function create() 
    { 
     $categories = Category::all(); 
     $colors = Color::all(); 
     $sizes = Size::all(); 
     $materials = Material::all(); 
     $fantasias = Fantasia::all(); 
     return view('backend.product.product-create', compact('categories','colors','sizes', 'materials', 'fantasias')); 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function store(UploadRequest $request) 
    { 
     $product = new Product(); 
     $product->product_name = $request->product_name; 
     $product->product_description = $request->product_description; 
     if($request->hasFile('product_preview')) { 
      $file = Input::file('product_preview'); 
      $filename = time(). '-' .$file->getClientOriginalName(); 
      $product->product_preview = $filename; 
      $file->move(public_path().'/images/product-feature', $filename); 
     } 
     $product->category_id = $request->category_id; 
     $product->color_id = $request->color_id; 
     $product->size_id = $request->size_id; 
     $product->material_id = $request->material_id; 
     $product->fantasia_id = $request->fantasia_id; 
     $product->save(); 


     if($request->hasFile('images')) { 
      $photos = Input::file('images'); 
      $file_count = count($photos); 
      $uploadcount = 0; 
      foreach($photos as $photo){ 
      $photoname = time(). '-' .$photo->getClientOriginalName(); 
      $photo->move(public_path().'/images/product-gallery', $photoname); 
      $uploadcount ++; 
      $productgallery = new ProductGallery(); 
      $productgallery->product_images = $photoname; 
      $productgallery->product_id = $product->id; // Save it to the newly created product 
      $productgallery->products()->associate($product); 
      $productgallery->save(); 
     } 
     } 
     if($uploadcount == $file_count){ 
     return $this->create()->with('success', 'Uploaded Successfully'); 
     } 
     else{ 
      return $this->create()->with('success', 'Uploaded fail'); 
     } 

    } 
    /** 
    * Display the specified resource. 
    * 
    * @param \App\Product $product 
    * @return \Illuminate\Http\Response 
    */ 
    public function show(Product $product) 
    { 
     // 
    } 

    /** 
    * Show the form for editing the specified resource. 
    * 
    * @param \App\Product $product 
    * @return \Illuminate\Http\Response 
    */ 
    public function edit(Product $product) 
    { 
     $categories = Category::all(); 
     $cats = array(); 
     foreach ($categories as $category) { 
     $cats[$category->id] = $category->category_name; 
     } 

     $colors = Color::all(); 
     $col = array(); 
     foreach ($colors as $color) { 
     $col[$color->id] = $color->color_name; 
     } 

     $materials = Material::all(); 
     $mat = array(); 
     foreach ($materials as $material) { 
     $mat[$material->id] = $material->material_type; 
     } 

     $sizes = Size::all(); 
     $si = array(); 
     foreach ($sizes as $size) { 
     $si[$size->id] = $size->size_name; 
     } 

     $fantasias = Fantasia::all(); 
     $fant = array(); 
     foreach ($fantasias as $fantasia) { 
     $fant[$fantasia->id] = $fantasia->fantasia_name; 
     } 

     if(!$product){ 
      return redirect('backend.dashboard')->with(['fail'=>'post not found']); 
     } 
     return view('backend.product.product-edit',compact('product', 'cats' , 'col' , 'mat', 'si', 'fant')); 
    } 

    /** 
    * Update the specified resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \App\Product $product 
    * @return \Illuminate\Http\Response 
    */ 
    public function update(Request $request, Product $product) 
    { 
     $this->validate($request, [ 
     'product_name'=>'required|max:120', 
     'product_preview' => 'required|file|image|mimes:jpeg,png,jpg,gif,svg', 
     ]); 

     $product->product_name = $request->product_name; 
     $product->product_description = $request->product_description; 
     if($request->hasFile('product_preview')) { 
     $file = Input::file('product_preview'); 
     $filename = time(). '-' .$file->getClientOriginalName(); 
     $file->move(public_path().'/images/product-feature', $filename); 
     $oldfile = $product->product_preview; 
     $product->product_preview = $filename; 
     $oldfiledelete = File::delete(public_path().'/images/product-feature', $oldfile); 
     } 
     $product->category_id = $request->category_id; 
     $product->color_id = $request->color_id; 
     $product->size_id = $request->size_id; 
     $product->material_id = $request->material_id; 
     $product->fantasia_id = $request->fantasia_id; 
     $product->update(); 
     return Redirect()->route('products.index')->with(['success'=> 'post successfully updated']); 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param \App\Product $product 
    * @return \Illuminate\Http\Response 
    */ 
    public function destroy(Product $product) 
    { 
     if(!$product){ 
      return redirect('products.index')->with(['fail'=>'post not found']); 
     } 
     $product->delete(); 
     return Redirect()->route('products.index')->with(['success'=> 'post successfully updated']); 
    } 
} 

產品create.blade.php(圖)

@extends('layouts.backend-master') 

@section('styles') 
    <link rel="stylesheet" href=""> 
@endsection 

@section('content') 

    @if (count($errors) > 0) 
    <div class="alert alert-danger"> 
     <strong>Whoops!</strong> There were some problems with your input.<br><br> 
     <ul> 
      @foreach ($errors->all() as $error) 
      <li>{{ $error }}</li> 
      @endforeach 
     </ul> 
    </div> 
    @endif 

    <h1>Add a new product</h1> 
    <form action="{{route('products.store')}}" method="post" enctype="multipart/form-data"> 

    <div class="input-group"> 
     <label for="product_name">Name of the product</label> 
     <input type="text" name="product_name" id="product_name"/> 
    </div> 

    <div class="input-group"> 
     <label for="product_description">Product Description</label> 
     <textarea type="text" name="product_description" id="product_description" rows="8"></textarea> 
    </div> 

    <div class="input-group"> 
     <label for="product_preview">Feature Image:</label> 
     <input type="file" name="product_preview" id="file"> 
    </div> 

    <div class="input-group"> 
     <label for="category_id">Category</label> 
     <select name="category_id" id="category_id"> 
     @foreach($categories as $category) 
      <option value="{{ $category->id }}">{{ $category->category_name }}</option> 
     @endforeach 
     </select> 
    </div> 

    <div class="input-group"> 
     <label for="color_id">Color</label> 
     <select name="color_id" id="color_id"> 
     @foreach($colors as $color) 
      <option value="{{ $color->id }}">{{ $color->color_name }}</option> 
     @endforeach 
     </select> 
    </div> 

    <div class="input-group"> 
     <label for="size_id">Size</label> 
     <select name="size_id" id="size_id"> 
     <option selected disabled>-</option> 
     @foreach($sizes as $size) 
      <option value="{{ $size->id }}">{{ $size->size_name }}</option> 
     @endforeach 
     </select> 
    </div> 

    <div class="input-group"> 
     <label for="material_id">Material</label> 
     <select name="material_id" id="material_id"> 
     <option selected disabled>-</option> 
     @foreach($materials as $material) 
      <option value="{{ $material->id }}">{{ $material->material_type }}</option> 
     @endforeach 
     </select> 
    </div> 

    <div class="input-group"> 
     <label for="fantasia_id">Model</label> 
     <select name="fantasia_id" id="fantasia_id"> 
     <option selected disabled>-</option> 
     @foreach($fantasias as $fantasia) 
      <option value="{{ $fantasia->id }}">{{ $fantasia->fantasia_name }}</option> 
     @endforeach 
     </select> 
    </div> 

    <div class="input-group"> 
     <label for="images">Product Gallery:</label> 
     <input type="file" name="images[]" multiple="true"> 
    </div> 

    <button type="submit" class="btn">Add</button> 
    <input type="hidden" name="_token" value="{{Session::token()}}"> 
    </form> 
@endsection 

@section('scripts') 
@endsection 

Products-table Products-galleries-table

預先感謝您

offline table structure

+0

如果您直播的網站有問題,然後它涉及到簡單的數據庫,我想你還沒有在該表的ID給自動遞增,因此這個問題,否則沒有辦法它會顯示爲0 – Exprator

+0

後重復條目截圖表結構.. –

+0

@ZaheerAttar,Zaheer Attar,完成 –

回答

1

看來increments()不適合你。

你可以試試unsignedInteger(),因爲它與Int類型創建一個列,並且還允許您與auto_increment約束創建它。它和increments()一樣,只是一個方法。您應該如下編寫代碼。

$table->unsignedInteger('id', true); 

這裏unsignedInteger()需要第一個參數爲string爲列的名稱,並且第二個參數作爲boolean爲AUTO_INCREMENT。試試這個,讓我們知道它是否有效。

相關問題