You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel修改返回路由问题:复制模型后跳转新模型编辑页失败

Fixing Redirect to New Model's Edit Page After Replication

Let's break down why your current approach isn't working and how to fix it—even with multiple entry points to the duplicate route.

The Problem with redirect()->back()

When you call redirect()->back(), Laravel simply sends the user back to the exact URL of their previous request—which is model/{original_model_id}/edit. There's no built-in logic to swap out the original model ID with the new duplicated one, so you end up on the old model's edit page instead of the new one.

Solution: Dynamically Modify the Previous Route

Since you can't hardcode a specific route (due to varied access scenarios), we'll dynamically grab the previous route's details, swap the model ID with the duplicated one, and redirect to that updated route.

Method 1: Use Route Name & Parameters (Most Reliable)

This approach leverages Laravel's route system to safely replace the model ID, avoiding accidental string replacements in URLs.

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;

// Your existing replication logic
$duplicate_model = $model->replicate();
$duplicate_model->save();

// Get the previous request and match it to a registered route
$previousRequest = request()->create(URL::previous());
$matchedRoute = Route::getRoutes()->match($previousRequest);

// Extract the route name and original parameters
$routeName = $matchedRoute->getName();
$routeParams = $matchedRoute->parameters();

// Replace the original model ID with the duplicated one
$routeParams['model'] = $duplicate_model->id;

// Redirect to the updated route
return redirect()->route($routeName, $routeParams)->with('model', $duplicate_model);

Why this works: It directly interacts with Laravel's route definitions, so it's safe even if your URL structure changes later. Just make sure all routes leading to the duplicate action have a name (which is best practice anyway).

Method 2: String Replacement (Simpler, Use with Caution)

If you don't want to rely on route names, you can directly replace the original model ID in the previous URL. Note this can cause issues if the original ID appears elsewhere in the URL (e.g., query parameters), but works well for simple URL structures.

use Illuminate\Support\Facades\URL;

// Your existing replication logic
$duplicate_model = $model->replicate();
$duplicate_model->save();

// Swap the original model ID with the new one in the previous URL
$originalUrl = URL::previous();
$newUrl = str_replace("/{$model->id}/", "/{$duplicate_model->id}/", $originalUrl);

// Redirect to the modified URL
return redirect($newUrl)->with('model', $duplicate_model);

Key Notes

  • Both methods adapt to any entry route (whether coming from edit, show, or another page) as long as the previous URL includes the original model ID in the path.
  • The with('model', $duplicate_model) will still work to pass the new model instance to the redirected page.

内容的提问来源于stack exchange,提问作者jacobdo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:12:03