跟踪从一个控制器到另一个控制器的变量

Keeping track of a variable from one controller to another

提前感谢您的帮助。

我目前正在学习Laravel,但我似乎无法解决问题。

我正在尝试为一家有经销商销售其产品的公司创建报价和发票解决方案。

所以我有一个销售人员使用表格创建了一个客户。输入存储在客户端 table.

Schema::create('clients', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id');
        $table->unsignedBigInteger('system_id');
        $table->string('name')->nullable();
        $table->string('contact')->nullable();
        $table->string('number')->nullable();
        $table->string('email')->nullable();

销售人员选择他们想向客户报价的“系统”的部分表格,这是上面的system_id

以上user_id仅指创建客户的销售人员。

这是我的产品migration

Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->integer('group');
        $table->string('code');
        $table->string('name');
        $table->double('price');
        $table->timestamps();
    });

客户端保存成功。之后我使用 if 语句来确定要遵循哪个 route 来配置系统。

if ($data['system_id'] == 1){
      return redirect(route('system.824'))->with('success', 'Customer details have been saved.');
  }elseif($data['system_id'] == 2){
      return redirect(route('system.32'))->with('success', 'Customer details have been saved.');
  }elseif ($data['system_id']==3){
      return redirect(route('system.500'))->with('success', 'Customer details have        been saved.');
  }

上面的 routes 转到我的 SystemsController 中的某个函数,在这里我检索了我的 products 迁移中上面提到的所需的“组”。

这些组用于配置systems

配置系统后,数据将发送回 SystemsController,以便我对接收到的输入执行所需的验证和配置的进一步逻辑。

系统配置完成后,它会被发送到 SystemsController,我需要在其中根据表单输入执行一些逻辑。

我的问题是访问 SystemsController 中的 client_id,这样我就可以将收到的输入存储到我创建的数据透视表 table 中。

client_id hasMany products products hasMany clients

我需要使用枢轴 table 来跟踪该客户端配置中引用的内容。数据透视表 table 将包含 client_id 和 product_id,最后一列用于保存数量。

我在这里错过了什么?

再次感谢。

如我的评论所述,您可以在 with 中发送更多数据,作为数组或连接更多 withs。

return redirect(route('system.32'))->with('success', 'Customer details have been saved.')->with('user_id', $userId);

return redirect(route('system.32'))->with(['success' => 'success message', 'user_id' => $userId]);

参考你的评论,如果你想在路由操作中传递数据,你需要像这样设置你的路由:

Route::get('path/to/your/route/system_input/{infoId}', 'SystemController@getInput')->name('system.input')

SystemController 中的方法应该接受参数。

public function getInput($infoId) { 

然后,在您的重定向路由中:

return redirect(route('system.input', ['infoId' => $info->id]) );

现在您可以通过 $infoIdgetInput 方法中访问 infoId。