当前位置: 首页 > 知识库问答 >
问题:

如何在设计表laravel上发布category_id

易扬
2023-03-14

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

产品和类别是多对多的关系产品属于设计师&designer拥有许多产品designer所属类别&类别有许多设计器

这是我的桌子

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

缺少is product need DesignerID
designer need category_id category need product_id如何在控制器上解决此问题谢谢

共有1个答案

孙凌龙
2023-03-14

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

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

如果还没有,还需要在product模型上设置一个关系。

public function designer() {
    return $this->belongsTo(Designer::class, 'designer_id');
}
 类似资料:
  • 我是新来的Laravel,遇到了以下问题: 我有一个表单应该在Post请求时重定向,但重定向没有发生。 这是我的表格: 这是CartController: 这是路线: 当我点击submit按钮时,URL会被产品数据填满,但我会在同一个页面上看到,而不是被重新定向到购物车。 我不知道为什么会发生这种事。这是一种至少能找出什么东西坏了的方法吗?我使用的是5.7版本。

  • 用例-我有一个包含两个文件的嵌入式框 > 一个大文件包含调试日志,用户日志协助技术领域的问题。这个大文件基本上是100Mb左右的zip文件。 另一个文件-包含配置的小文件-大约3 Kb。 我的要求是将这些文件发布到Web服务器。 技术-为了发布此文件,我使用libcurl。 我对网络服务器的假设——这可能是一个包含文件输入类型的表单。 此外,它可能正在运行节点服务器来处理POST请求。 问题 1)

  • 了解如何为开发人员发布设计规范。 利用设计规范,您可以与开发人员共享文件的设计属性。通过发布设计规范,您可以提供对象高度、宽度、大小、对齐方式、两个图标(对象)之间的相对间距、颜色和排版规则等关键信息。 将设计规范发布为公共链接时,您还可通过密码保护来限制访问。要对已经共享的设计规范进行密码保护,请参阅使用密码保护现有的原型链接。 将设计规范发布为私有链接时,您可以邀请内部和外部审阅者。 注意:您

  • 我正在尝试使用grpc实现pub-sub模式,但我对如何正确地实现它有点困惑。 我的协议:rpc调用(google.protobuf.Empty)返回(流数据) 客户: 服务器服务: 如何正确地从全局观察员中删除客户端?当连接中断时,如何接收某种信号 如何管理客户端-服务器重新连接?如何在连接断开时强制客户端重新连接? 提前感谢!

  • 本文向大家介绍.Net语言Smobiler开发之如何在手机上实现表单设计,包括了.Net语言Smobiler开发之如何在手机上实现表单设计的使用技巧和注意事项,需要的朋友参考一下 最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一、目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobiler Compone

  • 问题内容: 如何将UDP端口从Docker容器转发到主机? 问题答案: 使用-p标志并将后缀添加到端口号。 全指令 如果在Mac上运行boot2docker,请确保将boot2docker上的相同端口转发到本地计算机。 您还可以在以下位置暴露端口: 这是容器文档中包含更多Docker网络信息的链接:https : //docs.docker.com/config/containers/contai

  • 我有一个PHP laravel projekt,我需要添加一个字段到一个/多个模型(雄辩)。我在php方面没有太多的经验,以前从未尝试过laravel。 现在这个班看起来像这样 我想添加一个名为“level”的字符串字段,但我不知道如何操作。如果我先在MySQL中创建字段,然后更新模型,如果我更新模型,然后Laravel为我更新MySQL? 我期待着听到我能做什么:)

  • 我创建laravel项目,在我的本地Windows电脑上工作良好。一旦我上传到Centos7服务器(通过SSH),我的所有路由都不工作。我试图通过行动修复,例如清除缓存,删除供应商文件夹和重做安装作曲家,但没有任何帮助。我从下面的日志中抓取。 [2018-12-20 13:09:17]本地的。错误:逻辑异常:无法为序列化准备路由[api/user]。在 /var/www/html/srp/vend