你好我有3桌第1是产品第2类是第3类是设计师
产品和类别是多对多的关系 产品属于设计师和设计师拥有众多产品 设计师属于类别&类别有很多设计师
这是我的表
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('designers', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('slug')->unique();
$table->timestamps();
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
Schema::create('category_product', function (Blueprint $table) {
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')
->onDelete('restrict')
->onUpdate('restrict');
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products')
->onDelete('restrict')
->onUpdate('restrict');
$table->timestamps();
});
Schema::table('products', function (Blueprint $table) {
$table->integer('designer_id')->unsigned();
$table->foreign('designer_id')->references('id')->on('designers')
->onDelete('restrict')
->onUpdate('restrict');
});
Schema::table('designers', function (Blueprint $table) {
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')
->onDelete('restrict')
->onUpdate('restrict');
});
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;
foreach ($categoryname as $name) {
$category = category::firstOrNew(['name' => $name]);
$category->designer_id = $designer_id;
$category->save();
$category->products()->attach($product_id);
}
return Redirect::back()->with('status', 'Post Success');
缺少的是产品需要设计师 设计师需要category_id 类别需要product_id 怎么在控制器上解决这个问题谢谢
答案 0 :(得分:0)
除非您已将designer_id
添加到模型中的可填充数组中,否则无法以此方式更新designer_id
列。
或者,您只需执行$product->designer()->associate($designer);
而不是$product->designer_id = $designer->id
即可使用Eloquent方法。
如果您还没有,您还需要在Product
型号上设置关系。
public function designer() {
return $this->belongsTo(Designer::class, 'designer_id');
}