2017-08-29 95 views
-1

我有2個表稱爲產品和類別。產品屬於許多類別和類別,屬於多種產品。Laravel多對多關係語法錯誤

這裏是我的表模式:

Schema::create('products', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->timestamps(); 
     $table->string('image'); 
     $table->string('stock'); 
     $table->string('title'); 
     $table->string('slug')->unique(); 
     $table->string('gender'); 
     $table->text('description'); 
     $table->integer('price'); 
     $table->integer('user_id')->unsigned(); 
     $table->foreign('user_id')->references('id')->on('users') 
        ->onDelete('restrict') 
        ->onUpdate('restrict'); 

     $table->dateTime('published_at'); 
    }); 

這裏是

Schema::create('categories', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('categorydesigner_id')->unsigned()->index(); 
     $table->string('name')->unique(); 
     $table->timestamps(); 
    }); 

    Schema::create('category_product', function (Blueprint $table) { 
     $table->integer('product_id')->unsigned()->index(); 
     $table->integer('category_id')->unsigned()->index(); 
     $table->foreign('category_id')->references('id')->on('categories') 
        ->onDelete('restrict') 
        ->onUpdate('restrict'); 
     $table->foreign('product_id')->references('id')->on('products') 
        ->onDelete('restrict') 
        ->onUpdate('restrict'); 
     $table->timestamps(); 
    }); 

我收到以下錯誤

Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in 

C:\xampp\htdocs\swimwear2\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 681 

這是我控制我的類別表模式

public function productpost(Request $request){ 

    $this->validate($request, [ 
     'title' => 'required|max:255', 
     'description' => 'required', 
     'price' => 'required', 
     'image' => 'image|required', 
    ]); 

    $designer_name = $request->designer; 
    $designer_slug = str_random(40); 
    $designer = designer::where('name', $designer_name)->firstOrCreate(
      ['name' => $designer_name], ['slug' => $designer_slug] 
     ); 
    $designer->name = $designer_name; 
    $designer->slug = $designer_slug; 
    $designer->save(); 
    $designer_id = $designer->id; 
    $product = new Product; 
    $product->title = $request->title; 
    $product->designer_id = $designer_id; 
    $product->description = $request->description; 
    $product->price = $request->price; 
    $product->stock = $request->stock; 
    $product->gender = $request->gender; 
    $product_slug = str_random(40); 
    $product->slug = $product_slug; 
    $product->user_id = Auth::user()->id; 
    $product->published_at = Carbon::now()->format('Y-m-d'); 
    if($request->hasFile('image')) { 
     $file = Input::file('image'); 
     //getting timestamp 
     $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString()); 
     $name = $timestamp. '-' .$file->getClientOriginalName(); 
     $file->move(public_path().'/images/product/', $name); 
     $product->image = $name; 
     $thumb = Image::make(public_path().'/images/product/' . $name)->resize(1200,1800)->save(public_path().'/images/product/thumb/' . $name, 90); 
    } 
    $product->save(); 
    $productsearch = product::where('slug', $product_slug)->firstorfail(); 
    $product_id = $productsearch->id; 
    $categoryname = $request->category; 
    $category = category::where('name', $categoryname)->firstOrCreate(
      ['name' => $categoryname], ['categorydesigner_id' => $designer_id] 
     ); 

    $category->name = $category; 

    $category->categorydesigner_id = $designer_id; 
    $category->save(); 

    $category->products()->attach($product_id); 
    return Redirect::back()->with('status', 'Post Success'); 
} 

我的產品和設計器成功添加,僅在類別請求中出錯。 designerid會好起來的。

$category = category::where('name', $categoryname)->firstOrCreate(
      ['name' => $categoryname], ['categorydesigner_id' => $designer_id] 
     ); 

    $category->name = $category; 

    $category->categorydesigner_id = $designer_id; 
    $category->save(); 

    $category->products()->attach($product_id); 
+0

您可以嘗試刪除firstOrCreate方法之前的where子句嗎?第一個參數就像一個地方。 也刪除$ category-> name,$ category-> categorydesigner_id和$ category-> save()行,這些​​都是不必要的,可能是問題的一部分(雖然不知道)。 – Alexej

+0

感謝它現在修復 –

+0

我將提交它作爲您的問題的答案 – Alexej

回答

1

刪除firstOrCreate方法之前的where子句。 第一個參數就像一個地方。

同時刪除$category->name,$category->categorydesigner_id$category->save()行,這些都是不必要的,可能是問題的一部分。