php 重定向至路线时不显示 Flink 消息

mwg9r5ms  于 2023-02-03  发布在  PHP
关注(0)|答案(3)|浏览(127)

我使用以下代码将联系人与销售线索关联起来:

  1. public function selectSalesConsultant($uuid)
  2. {
  3. $lead = Lead::where('uuid', $uuid)->firstOrFail();
  4. return view('dealer-contacts.select_consultant')
  5. ->with(compact('lead'));
  6. }
  7. public function updateSalesConsultant(Request $request, $uuid)
  8. {
  9. $lead = Lead::where('uuid', $uuid)->firstOrFail();
  10. $lead->update([
  11. 'contact_id' => $request->get('contact_id')
  12. ]);
  13. Flash::success('Sales Consultant information updated for the lead.');
  14. return to_route('select-sales-consultant', ['uuid' => $uuid]);
  15. }

在我的第二个函数中,在更新销售线索后,我希望显示一条成功消息:

  1. Flash::success('Sales Consultant information updated for the lead.');
  2. return to_route('select-sales-consultant', ['uuid' => $uuid]);

以下是我的看法:

  1. @include('flash::message')

下面是在routes/api.php中定义路由的方式:

  1. Route::get('/select-sales-consultant/{uuid}', [App\Http\Controllers\LeadController::class, 'selectSalesConsultant'])
  2. ->name('select-sales-consultant');

如果我没有重定向,而是执行return view(),则会显示消息。
正确的方法是什么?我使用的是Laracasts Easy flash notifications包。

xt0899hw

xt0899hw1#

尝试将代码更改为:

  1. Flash::success('Sales Consultant information updated for the lead.');
  2. return redirect()->route('select-sales-consultant', ['uuid' => $uuid])
  3. ->with(['message' => Flash::message()]);

with方法允许您为单个请求向会话传递数据,这样消息在重定向后将在会话中可用,然后,您可以在视图中显示消息。
希望这能解决你的问题。

vbopmzt1

vbopmzt12#

看起来是因为我用了API路线。
我将以下代码添加到我的Kernel.php中,它解决了这个问题。

  1. 'api' => [
  2. \Illuminate\Session\Middleware\StartSession::class,
  3. //etc
  4. ],
e5njpo68

e5njpo683#

请检查应用程序/Http/内核. php

  1. protected $middlewareGroups = [
  2. 'web' => [
  3. ...
  4. \Illuminate\Session\Middleware\StartSession::class,
  5. ...
  6. ],
  7. ....
  8. ];

然后尝试使用quest->session()->flash()函数。

  1. public function updateSalesConsultant(Request $request, $uuid)
  2. {
  3. ...
  4. //Flash::success('Sales Consultant information updated for the lead.');
  5. $request->session()->flash('success', 'Sales Consultant information updated for the lead.');
  6. ...
  7. }
展开查看全部

相关问题