如何 post category_id on desinger table laravel

how to post category_id on desinger table laravel

嗨,我有 3 个 table 第一个是产品,第二个是类别,第三个是设计师

产品和类别是多对多关系 product belongsto designer & designer has许多产品 设计师属于类别 & 类别有许多设计师

这是我的 table

产品

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');
    });

设计师table

Schema::create('designers', function (Blueprint $table) {
        $table->increments('id');

        $table->string('name')->unique();
        $table->string('slug')->unique();
        $table->timestamps();
    });

类别table

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

在产品中添加缺失的列 table

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');

缺少产品需要 designerid
设计师需要 category_id 类别需要 product_id 如何在控制器上解决这个问题谢谢

您不能以这种方式更新 designer_id 列,除非您已将 designer_id 添加到模型中的可填充数组中。

或者,您可以使用 Eloquent 方法,只需执行 $product->designer()->associate($designer); 而不是 $product->designer_id = $designer->id

如果您还没有,您还需要在 Product 模型上建立关系。

public function designer() {
    return $this->belongsTo(Designer::class, 'designer_id');
}